user1206480
user1206480

Reputation: 1858

onclick method is not working in Blazor server-side Razor component

I am building a sample Razor component, and I can not get my button onclick method to fire. When I click the button, nothing happens at all. I have even placed a breakpoint in the method to see if it catches which it doesn't. The component does render, but as I said, the LoginUser method doesn't appear run at all when clicking the button.

Razor component page:

<div class="text-center">
    <Login FieldsetAttr="fieldsetAttr" UsernameAttr="usernameAttr" PasswordAttr="passwordInput"
           ButtonAttr="buttonAttr" ButtonText="Sign In" SignInManager="SignInManager"
           InvalidAttr="invalidAttr" />

</div>

@code {
    Dictionary<string, object> fieldsetAttr =
        new Dictionary<string, object>()
        {
            {"class", "form-group" }
        };

    Dictionary<string, object> usernameAttr =
        new Dictionary<string, object>()
        {
            {"class", "form-control" },
            {"type", "text" },
            {"placeholder", "Enter your user name here." }
        };

    Dictionary<string, object> passwordInput =
        new Dictionary<string, object>()
        {
            {"class", "form-control" },
            {"type", "password" }
        };

    Dictionary<string, object> buttonAttr =
        new Dictionary<string, object>()
        {
            {"class", "" },
            {"type", "button" }
        };

    Dictionary<string, object> invalidAttr =
        new Dictionary<string, object>()
        {
            {"class", "invalid-feedback" }
        };

}

Razor component:

<div @attributes="FormParentAttr">
    <form @attributes="LoginFormAttr">
        <fieldset @attributes="FieldsetAttr">
            <legend>Login</legend>
            <label for="usernameId">Username</label><br />
            <input @attributes="UsernameAttr" id="usernameId" @bind="UserName" /><br />
            <label for="upasswordId">Password</label><br />
            <input @attributes="PasswordAttr" id="passwordId" @bind="Password" /><br />
            <button @attributes="ButtonAttr" @onclick="LoginUser">@ButtonText</button>
            @if(errorMessage != null && errorMessage.Length > 0)
            {
                <div @attributes="InvalidAttr">
                    @errorMessage
                </div>
            }
        </fieldset>
    </form>
</div>

@code {
    [Parameter]
    public Dictionary<string, object> FormParentAttr { get; set; }

    [Parameter]
    public Dictionary<string, object> LoginFormAttr { get; set; }

    [Parameter]
    public Dictionary<string, object> FieldsetAttr { get; set; }

    [Parameter]
    public Dictionary<string, object> UsernameAttr { get; set; }

    [Parameter]
    public Dictionary<string, object> PasswordAttr { get; set; }

    [Parameter]
    public Dictionary<string,object> ButtonAttr { get; set; }

    [Parameter]
    public SignInManager<IdentityUser> SignInManager { get; set; }

    [Parameter]
    public Dictionary<string, object> InvalidAttr { get; set; }

    private string UserName { get; set; }
    private string Password { get; set; }

    [Parameter]
    public string ButtonText { get; set; }

    private string errorMessage { get; set; }

    private async Task LoginUser(MouseEventArgs e)
    {
        var user = new IdentityUser(UserName);

        var loginResult = await SignInManager.CheckPasswordSignInAsync(user, Password, true);

        if(loginResult.Succeeded)
        {
            await SignInManager.SignInAsync(user, true);
            errorMessage = "";
        }
        else
        {
            errorMessage = "Username or password is incorrect.";
        }
    }
}

Upvotes: 13

Views: 14798

Answers (6)

Skippy VonDrake
Skippy VonDrake

Reputation: 846

Had the same issue (.NET 8.) with OnClick after copying a razor page. Added "@rendermode InteractiveServer" to top of page which fixed it. More details found at https://github.com/dotnet/aspnetcore/issues/50712

Upvotes: 4

painfuldip
painfuldip

Reputation: 11

After following this tutorial, I was getting a 404 error stating my _framework/blazor.server.js file could not be found. The solution came down to adding "~/" before the _framework/blazor.server.js.

Upvotes: 1

Hugh Jones
Hugh Jones

Reputation: 2694

Please also be aware that Internet Explorer also can result in similar behaviour.

I guess it is blocking JavaScript code...

This took me longer to realise than perhaps it should have.

Upvotes: 0

Gnyasha
Gnyasha

Reputation: 694

I faced the same issue, but I resolved it by changing ServerPrerendered to Server".

In your _Host.cshtml, change this

<component type="typeof(App)" render-mode="ServerPrerendered" />

to

<component type="typeof(App)" render-mode="Server" />

Upvotes: 3

itzprintz
itzprintz

Reputation: 126

Make sure you

1. Configure Blazor in the Startup.cs file:

In public void ConfigureServices(IServiceCollection services) you call services.AddServerSideBlazor(); (...for server-side Blazor)

and in public void Configure(IApplicationBuilder app, IWebHostEnvironment env) you register the endpoint, such as ('endpoint.MapBlazorHub()' is what you need to add here):

app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapBlazorHub();
        });

2. You have

@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web

in the Razor component where you're using Blazor.

Upvotes: 5

RyanY
RyanY

Reputation: 849

This happened to me in an ASP.NET Core project where I was trying to add Blazor components following this blog's guidance. After comparing my project with the template Blazor project, I discovered that it was the missing _Imports.razor file that was causing blazor.server.js to not subscribe to the click event. I added that file to my project just as it is in the template project:

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop

Button clicks were then working as expected.

Upvotes: 23

Related Questions