MaxTE7
MaxTE7

Reputation: 21

Executing async method on button click Blazor Server

I am trying to execute an asynchronous method when pressing a button. The code compiles without issue, but when the button is clicked the method is never called. I have used all the google fu i have at my disposal to no avail. Am I doing something wrong syntactically? Did i forget to import something or am i misunderstanding how this works?

  @foreach (Data.Course cor in CourseList)
                    { 
/...
<button class="btn btn-outline-primary" @onclick="@(async () => await EnrollCourse(cor.CourseId))">
                                        Enroll
                                    </button>
}

@functions{
    private async Task EnrollCourse(int corid)
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        string userid = authState.User.Identity.Name;
        await _db.EnrollCourse(corid, userid);
        NavigationManager.NavigateTo($"/course/{corid}");
        
    }



}

Upvotes: 1

Views: 2610

Answers (2)

MaxTE7
MaxTE7

Reputation: 21

For anyone that comes around to this on google, i ended up setting the button to redirect to a different page that runs the code and redirects to the final page.

<div class="button">
    <a href="/enroll/@cor.CourseId">Enroll</a>
</div>
@page "/enroll/{course}"
@inject Data.CourseData _db
@inject NavigationManager NavigationManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject Microsoft.AspNetCore.Identity.UserManager<Microsoft.AspNetCore.Identity.IdentityUser> userManager
@using System.Security.Claims


@code {
    [Parameter]
    public string course { get; set; }
    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        var user = await userManager.GetUserAsync(authState.User);
        if (user != null)
        {
            string userid = user.Id;
            await _db.EnrollCourse(Int32.Parse(course), userid);
            NavigationManager.NavigateTo("/course/" + Int32.Parse(course));


        }
    }

}

Upvotes: 1

Brian Parker
Brian Parker

Reputation: 14553

Please note how I am collecting the User Id. I hope this helps. Your user manager may be of type UserManager<ApplicationUser> as well...

    @inject UserManager<ApplicationUser> userManager
    @using System.Security.Claims
    .....
    <AuthorizeView>
          @foreach (Data.Course cor in CourseList)
                    { 
/...
<button class="btn btn-outline-primary" @onclick="@(async () => await EnrollCourse(cor.CourseId,context.User))">
                                        Enroll
                                    </button>
}
    </AuthorizeView>
@functions{
     private async Task EnrollCourse(int corid, ClaimsPrincipal identity)
     {

        var user = await userManager.GetUserAsync(identity);

        var userid = user.Id;
        await _db.EnrollCourse(corid, userid);
        NavigationManager.NavigateTo($"/course/{corid}");
        
     }



}

Upvotes: 0

Related Questions