Home > database >  refresh Blazor page
refresh Blazor page

Time:12-31

In the Blazer Server Side project with .NET 6, I am on the following page:

https: //localhost: 7252/Product/a3620f82-7cba-473c-9273-1cf300a181eb

I have a NavLink on this page that points to the exact same URL above. But when I click on the method, the page does not show any reaction and does not refresh or update. What should I do?

Sample my codes:

@page "/Product/{Id:guid}"
@using Application.ProductGalleries.Queries
@using Application.Products.Queries
@using ViewModels.ProductVariants
@using ViewModels.ProductVariantsDetails
@using ViewModels.Products
<div >
                        <h1>
                            <NavLink href="@($"/Product/a3620f82-7cba-473c-9273-1cf300a181eb")">@Model.ProductName</NavLink>
                            </h1>
                        <p>دسته بندی : @Model.CategoryName<span> برند: @Model.BrandName</span></p>
                        <nav aria-label="breadcrumb">
                            <ol >
                                <li ><NavLink href="/">صفحه نخست</NavLink></li>
                                <li ><NavLink href="/Products">@Model.CategoryName</NavLink></li>
                                <li  aria-current="page">@Model.BrandName</li>
                            </ol>
                        </nav>
                    </div>


@code {
[Parameter]
public Guid Id { get; set; }
protected override Task OnParametersSetAsync()
{
    return base.OnParametersSetAsync();
}
protected override Task OnAfterRenderAsync(bool firstRender)
{
    return base.OnAfterRenderAsync(firstRender);
}

protected override bool ShouldRender()
{
    return base.ShouldRender();
}
protected override void OnAfterRender(bool firstRender)
{
    base.OnAfterRender(firstRender);
}
protected override void OnInitialized()
{
    base.OnInitialized();
}

protected override void OnParametersSet()
{
    base.OnParametersSet();
}

public override Task SetParametersAsync(ParameterView parameters)
{
    return base.SetParametersAsync(parameters);
}
protected async override Task OnInitializedAsync()
{
    var request = new GetProductByIdQuery() { ProductId = Id };

    var product = await Mediator.Send(request: request);
    if (product.Value != null)
    {
        Model = product.Value;
        GalleryModel = await GetGallery();

    }
}
}

During the search, if the user has searched for bananas and is on the search page and wants to search for bananas again, the page will not show any reaction. I went through all the life cycle overrides to see where it was going but it didn't react.

I also thought of using StateHasChanged (). But my code is a NavLink and where should I put this code when the project does not enter the life cycle?

CodePudding user response:

... and is on the search page and wants to search for bananas again, the page will not show any reaction.

The OnInitialized event is only executed once. The life-cycle event you are after is OnParametersSet:

protected async override Task OnInitializedAsync()
{
   // nothing to do here
}

protected override async Task OnParametersSetAsync()
{
    var request = new GetProductByIdQuery() { ProductId = Id };

    var product = await Mediator.Send(request: request);
    if (product.Value != null)
    {
        Model = product.Value;
        GalleryModel = await GetGallery();    
    }
}

CodePudding user response:

Try following code:

@code {

    void NevigateToYourUrl()
    {
        _navigationManager.NavigateTo("YourPagePath");
    }
}

and also you need to inject NavigationManager as follow:

@inject NavigationManager _navigationManager

Try:

<a href="" @onclick="@NevigateToYourUrl" @onclick:preventDefault />
  • Related