0
votes

I'm using Blazor Server for website with paginated list with listings/search results (as in any normal online directory website). I'm wondering what's the best fix for the scenario below:

  1. I have a Search component which takes category name as parameter plus Query String so the url can look like this https://example.com/cars?loc=london?text=silver

  2. It can give me more than 10 results so I added pagination.

  3. When I go to next page the url will add page number e.g https://example.com/cars?loc=london?text=silver?page=3

My problem is that when I go to the component for the first time it executes OnInitializedAsync() and OnParametersSetAsync() as expected.

When I click on the next/previous page number I have the code below but the Navigation manager is not calling the OnInitializedAsync() and OnParametersSetAsync() methods again so I can't get the new list based on page number (I can only see the page number in url but nothing happens).

Can I force Blazor to run OnInitializedAsync() and OnParametersSetAsync() again or am I doing it wrong?

public void PageIndexChanged(int newPageNumber)
{
    if (newPageNumber < 1 || newPageNumber > totalPages)
    {
        return;
    }
    currentPage = newPageNumber;
    //...
    query.Add("page", currentPage.ToString());

    NavigationManager.NavigateTo(QueryHelpers.AddQueryString(categoryUrl, query));
}
1
OnInitialized(Async) are executed only once, when the component is created. OnParametersSet(Async) are called whenever a parameter should be set. I'd suggest you post your whole code so we can inspect it, and tell you how to make it work - enet
basically I would like to know why this is not doing anything (other than changing url in the address bar). NavigationManager.NavigateTo(QueryHelpers.AddQueryString(categoryUrl, query)); I needed to add this line below to get updated results: await GetListings(categoryUrl, searchLocation, searchText, currentPage); Component route is @page "/{categoryUrl}" I can add a full code but there is really not much more - skynetPL
Do you need the page number in the URL? If that isn't required, this can be done much easier. - Nik P
I'm using page in url when user goes to listing details and then clicks on retrun in the browser so I know where he was on the list (using query.TryGetValue("page", out var pageNo)). I know it would be easier with normal web application so I don't know if or how I can achieve the same with Blazor - skynetPL

1 Answers

0
votes

Hy ,

This is a longshot, butt did you try ;

navigationManager.NavigateTo(url,true)

The true at the end forces the navigation manager to (re)load the page.

Navigation Manager info

regards,