Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 669
niico Avatar asked Dec 30 '25 14:12

niico


1 Answers

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" />
like image 80
Ruikai Feng Avatar answered Jan 01 '26 07:01

Ruikai Feng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!