11

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 break point 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 componet 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.";
        }

    }
}
user1206480
  • 1,798
  • 3
  • 28
  • 45
  • Do you get an error message in the browser console? (F12 in the browser) – Pascal R. Oct 14 '19 at 06:35
  • I have the same issue. The debugger does not break and there is no message in the debugger, even when setting the debug level to 0. – MovGP0 Oct 14 '19 at 11:34
  • Check that you don't have `RenderMode.Static` in your `_Host.cshtml` file. – Pascal R. Oct 14 '19 at 13:59
  • And I expect you have the latest version .Net Core 3.0 installed. – Pascal R. Oct 14 '19 at 14:04
  • @PascalR. The app is 3.0, render mode is RenderMode.ServerPrerendered, and I am not seeing any errors in the browser. I am totally stumped on this. I've tried simplifying my onclick method a few different ways by making it a regular void non-async method, and it still didn't work. I'm sure it must be something obvious that I am overlooking, but I simply can't figure it out. – user1206480 Oct 14 '19 at 15:14
  • It appears that a simple css attribute was causing the problem or preventing the method from executing. Really weird, not sure exactly why. I will be deleting the post shortly in light of the issue. I did come across another issue now which I've posted here https://stackoverflow.com/questions/58387683/signin-for-blazor-server-side-app-not-working . The method now executes, but hangs once it hits the SignInAsync method. – user1206480 Oct 15 '19 at 05:23
  • @user1206480 I've the same probleme in an application where i try to add the Blazor Server Side stuff. I've already removed alle css attributes but no success. Any hint what you've done to solve it? – M. Altmann Oct 17 '19 at 15:06
  • @maltmann I removed a bootstrap class on a div tag, and then the method fired. I have no idea why this was. Doesn't make much since to me at all. I was just satisfied to get on the right track, and would try to figure it out later. – user1206480 Oct 18 '19 at 04:40
  • I opened another question for my problem and got the solution https://stackoverflow.com/questions/58436408/blazor-features-missing-in-existing-project/58444800#58444800 – M. Altmann Oct 18 '19 at 06:08

5 Answers5

19

This happened to me in an MVC 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.

RyanY
  • 749
  • 1
  • 6
  • 16
  • 3
    Yes! This has been killing me for a day. This solved the same problem I was having, and should definitely be the accepted answer. – Marc Chu Aug 25 '20 at 11:53
5

Make sure you

1) Configure Blazor in 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 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 Razor component where you're using Blazor.

itzprintz
  • 106
  • 1
  • 5
3

Faced the same issue but 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" />
Gnyasha
  • 658
  • 11
  • 19
  • I'm doing Blazor SSR .NET 7. Changing ServerPrerendered to Server caused my Blazor SSR app to stop working - never even showed the first/home page. – StackOverflowUser Aug 28 '23 at 22:06
0

Please also be aware that IE also can result in similar behaviour.

I guess it is blocking Js code...

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

Hugh Jones
  • 2,706
  • 19
  • 30
0

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