Counter page in blazor not working

MIPAKTEH_1 365 Reputation points
2024-06-04T15:02:08.19+00:00

This code pick from somewhere but not count when Click event in Counter page.

can tell why.

@page "/counter"

<h1>Counter</h1>

<p>The counter value is @currentCount</p>

<button class="btn btn-primary" @onclick=IncrementCounter>Increment counter</button>

@code {

private int currentCount = 42;

private void IncrementCounter()

{

    currentCount++;

}

}

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 4,720 Reputation points Microsoft Vendor
    2024-06-05T01:37:10.38+00:00

    Hi @MIPAKTEH_1,

    Static rendering is the default in .NET 8. .NET 8 introduced the notion of Render Modes, and it is now required either at the page level, or globally. The default is static rendering which means that the component is rendered exactly as it is written with no interactivity being wired up by the Blazor engine.

    To wire up interactivity, you have two options.

    Per component solution Check for this directive at the top of the file(Counter.razor here):

    @rendermode InteractiveServer
    

    Global solution Update Routes element in App.razor file:

    <body>
        <Routes @rendermode="new InteractiveServerRenderMode()"  />
    </body>
    

    Be sure import the namespace in _Imports.razor:

    @using static Microsoft.AspNetCore.Components.Web.RenderMode
    

    Note: Even though you have added AddInteractiveServerComponents() this does NOT mean that you have set the default to be InteractiveServer. It only means that you have the option to add InteractiveServer as a global or component option. So you still must add the global or component option as above.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Rena

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,866 Reputation points
    2024-06-05T00:30:23.1033333+00:00

    Must likely the default render is static, and no events are supported. Add a render mode to the page, or change the default.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.