Search code examples
asp.net-coreblazorblazor-server-side.net-8.0blazor-auto

Server Side Blazor Component in .net 8, @onclick event not firing


In Blazor Server in .net 7 I can do this in a Page.blazor:

@message

<button class="btn btn-primary mt-2 btn-block" @onclick="SendMessage">Send Message</button>


@code {
    public string message { get; set; }

    private async Task SendMessage()
    {
        message = "Hello";
    }
}

When you click the button it outputs "Hello" on the page.

However, in .net 8 in a "Blazor Web App" (Auto, Server mixed with Web Assembly) in a Page.blazor on the server side, this button doesn't work.

Buttons in an EditForm do work but I don't want to use that, I just want clicking a button to fire an event.

Why doesn't this work in .net 8 and how can I do this?

Thanks


Solution

  • The rendermode would be Static Server by default,here's the document related

    add @attribute [RenderModeInteractiveServer] in your component

    or

    add the codes below in app.razor for entire app

    @using static Microsoft.AspNetCore.Components.Web.RenderMode
    .....
    <Routes @rendermode="InteractiveServer" />