Ah Blazor - Now refresh the page!

Started my first Blazor project today.  New to NetCore as well so it was a fun challenge.  I like that is is similar to MVC  and I appreciate  the ability to add code into my page while building without moving to a controller - it was a real challenge to accommodate some JQuery but I got everything to work.  After looking a little closer I realized a large chunk of my JQuery and even javascript could be moved to the @code section and written differently - made for a cleaner, more readable page.

I spent over an hour trying to figure out how to do a simple page refresh!  Part of the challenge is that Blazor is a moving target - different libraries and ways of doing simple tasks from version to version - I am confident that most of that will stop as they more clearly define what it is they want to do with this tool.

Refresh a page (NetCore 3.1)

First set up an alias for the uriHelper - you will now find it in NavigationManager
@inject NavigationManager uriHelper

Attach a method to your button:

  <button type="submit" @onclick="Reload">Start Over </button>

In your @code  section add this oh so simple method - you will want to include the true as it initiates a real reload otherwise you will just navigate to the same location with no StateChange.  There are most likely many ways to do this - this was the easiest.


    private async void Reload()
    {
        uriHelper.NavigateTo("/MyPage", true);
     
    }

Comments

Popular posts from this blog

Linq Exclude from separate list

Sorting Ascending and Descending

Linq Query Syntax vs Method Syntax