niico
niico

Reputation: 12749

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

Upvotes: 13

Views: 9218

Answers (2)

Abel Dantas
Abel Dantas

Reputation: 430

Another alternative without changing the render mode:

<form @onsubmit="SendMessage" @formname="SendMessage" method="post">
    <button class="btn btn-primary">Approve</button>
    <AntiforgeryToken/>
</form>

Upvotes: 5

Ruikai Feng
Ruikai Feng

Reputation: 11826

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" />

Upvotes: 18

Related Questions