Blazor Update NavMenu

Franck J 1 Reputation point
2021-11-25T15:45:30.163+00:00

Hello

I am looking for updating the NavMenu.razor menu, from information obtained in the pages, via a session
I have a project that references Blazored.SessionStorage

The SetItemAsync method works perfectly.
However, when I try to retrieve the information by GetItemAsync or ContainKeyAsync, the program stop

The program is available on
https://github.com/actiproj/TestSessionNavMenu

Kind regards

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,406 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,021 Reputation points Microsoft Vendor
    2021-11-26T03:09:56.1+00:00

    Hi @Franck J ,

    According to your sample code, you are creating a Blazor Server application and use the Blazored SessionStorage, right?

    I have tested your code on my side, if I set debugger on the GetItemAsync or ContainKeyAsync part of the code in the NavMenu.razor page, the session storage related code will not run continue and there is no error and exception. But if we move this part of code from the RecupInfo method to the OnAfterRender method, everything works well.

    152783-image.png

    The result is like this:

    152719-image.png

    So, the issue might relate the Blazored SessionStorage package, it not working in the onchange event. But from the usage of the Blazored SessionStorage, we can't find any description about that.

    As a workaround, you could try to move the GetItemAsync or ContainKeyAsync part of the code from the RecupInfo method to the OnAfterRender method. And you could also submit feedback to Blazored SessionStorage.

    Besides, you could also try to use the Protected Browser Storage to store the data, see ASP.NET Core Protected Browser Storage


    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

  2. Prathamesh Shende 376 Reputation points
    2021-12-04T11:08:51.257+00:00

    There must be problem with the class that you use the how session is going to save in class object. You need to call by string or int
    in GetItemAsync<int> like this

    0 comments No comments