how to refresh component in razor pages?

mc 6,396 Reputation points
2023-02-15T04:52:03.16+00:00

I create component of Counter

and use in it razor pages as static

<component type="typeof(Components.Counter)" render-mode="Static" param-start="10"/>

how to refresh it partialy not refresh the page?

I want it refresh when I change the parameter

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-02-16T03:00:54.5166667+00:00

    Hi @打玻璃

    and use in it razor pages as static <component type="typeof(Components.Counter)" render-mode="Static" param-start="10"/> how to refresh it partialy not refresh the page?

    To solve this issue, you need to Change the Render Mode from Static to ServerPrerendered: because when using the static mode, it will render the component into static HTML.

    Based on your description, I create a sample use the following code, you can refer to it:

    In the Program.cs file, add the MapBlazorHub middleware and AddServerSideBlazor service:

    builder.Services.AddServerSideBlazor(); 
    
    app.MapBlazorHub();
    

    In the _Layout.cshtml page, add the _framework/blazor.server.js script reference:

    263793-image.png

    In the Counter component, add the Microsoft.AspNetCore.Components and Microsoft.AspNetCore.Components.Web reference:

    @using Microsoft.AspNetCore.Components
    @using Microsoft.AspNetCore.Components.Web
    <h1>@Title</h1>
    
    <p role="status">Current count: @currentCount</p>
    <p>Increment amount: @IncrementAmount</p>
    
    <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
    
    @code {
        private int currentCount = 0;
    
        [Parameter] public string Title { get; set; } = "Blazor Counter";
        [Parameter] public int? IncrementAmount { get; set; }
    
        private void IncrementCount()
        {
            currentCount += IncrementAmount.GetValueOrDefault(1);
        }
    }
    

    In the Razor page, use the following code to display the component:

    <component type="typeof(Components.Counter)" render-mode="ServerPrerendered" param-IncrementAmount="2" />  
    

    The result as below:

    image1


    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,

    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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