Maverick (Shaun)
Maverick (Shaun)
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
Referring to the code below:
private async void PublishDrawWithConfirmation()
{
var result = await DialogService.ShowMessageBox(
"Publish Draw",
"Once published, the draw will no longer be editable and will be open to entries. Are you sure?",
yesText: "Publish", cancelText: "Cancel");

if (result == null) return;

await DrawService.ActivateDraw(DrawId);
Snackbar.Add("Draw has been published!", Severity.Success);
StateHasChanged();
}
private async void PublishDrawWithConfirmation()
{
var result = await DialogService.ShowMessageBox(
"Publish Draw",
"Once published, the draw will no longer be editable and will be open to entries. Are you sure?",
yesText: "Publish", cancelText: "Cancel");

if (result == null) return;

await DrawService.ActivateDraw(DrawId);
Snackbar.Add("Draw has been published!", Severity.Success);
StateHasChanged();
}
My intent is to refresh the page after updating the database, in order to display the updated information. Based on the behaviour I am seeing, it looks as if StateHasChanged() is invoked before the database is updated with await DrawService.ActivateDraw(DrawId). What would be the correct way to refresh the page only after the await is complete?
20 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
What is the most straightforward way to handle multiple Blazor components trying to access the database via a DbContext in EF Core at once? I'm creating an indeterminate number of components on a page at once that all make a call within OnInitializedAsync() to a scoped service in order to pull a record from the database. A problem seems to arise from the fact that they are all using the same DbContext, which is not thread safe? Can someone point me in the right direction? Hopefully that's not too broad a question, still new to this area .
21 replies
CC#
Created by Maverick (Shaun) on 2/5/2024 in #help
Blazor Web App (.NET 8) - Logout functionality with IdentityCore
What's the best way to implement log out functionality? Looks like I can't use signInManager.SignOutAsync() on an InteractiveServer application
9 replies
CC#
Created by Maverick (Shaun) on 2/4/2024 in #help
Using IdentityCore with InteractiveServer rendermode (Blazor .NET 8)
I've realised that the IdentityCore login/register pages do not work with InteractiveServer rendermode. I've worked around this by setting the rendermode conditionally rather than globally (InteractiveServer for everything BUT these pages). However, that of course breaks the interactivity of my MainLayout navigation. Can anyone suggest a good way to approach this problem? Ideally, I'd like to use the default login/register pages (with a few tweaks) and have the interactivity of MainLayout still work. I'm a newcomer to Blazor and .NET so I'm not sure what my options are. Any advice is welcome, thanks in advance.
11 replies
CC#
Created by Maverick (Shaun) on 2/1/2024 in #help
Default parameter values in primary constructor
I've created a data model class for use with Entity Framework Core, and I just had a question regarding my use of a primary constructor here. As you can see, IsActive and Prizes are both being initialized to default values. Will this give me the same behaviour as if I'd used an old-school constructor? (i.e. public RaffleModel(...)... you know the drill) Additionally, are there any considerations I need to have specifically relating to how Entity Framework Core will interact with this class?
public class RaffleModel
(string title, string description, DateTime drawDate, bool isBundle, string raffleHolderId)
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RaffleId { get; set; } // auto-generated

[Required]
[StringLength(30)]
public string Title { get; set; } = title;

[Required]
[StringLength(500)]
public string Description { get; set; } = description;

[Required]
public DateTime DrawDate { get; set; } = drawDate;

// Set to true when the raffle is published (after prizes are added)
[Required]
public bool IsActive { get; set; } = false;

[Required]
public bool IsBundle { get; set; } = isBundle;

[ForeignKey("AspNetUsers")]
public string RaffleHolderId { get; set; } = raffleHolderId;

// Identity user reference
public virtual ApplicationUser? RaffleHolder { get; set; }

// Navigation property (initialised to prevent null references)
public virtual ICollection<PrizeModel> Prizes { get; set; } = new HashSet<PrizeModel>();
}
public class RaffleModel
(string title, string description, DateTime drawDate, bool isBundle, string raffleHolderId)
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RaffleId { get; set; } // auto-generated

[Required]
[StringLength(30)]
public string Title { get; set; } = title;

[Required]
[StringLength(500)]
public string Description { get; set; } = description;

[Required]
public DateTime DrawDate { get; set; } = drawDate;

// Set to true when the raffle is published (after prizes are added)
[Required]
public bool IsActive { get; set; } = false;

[Required]
public bool IsBundle { get; set; } = isBundle;

[ForeignKey("AspNetUsers")]
public string RaffleHolderId { get; set; } = raffleHolderId;

// Identity user reference
public virtual ApplicationUser? RaffleHolder { get; set; }

// Navigation property (initialised to prevent null references)
public virtual ICollection<PrizeModel> Prizes { get; set; } = new HashSet<PrizeModel>();
}
55 replies