Maintaining the state of the DOM after switching between pages RRS feed

  • Question

  • User1960406488 posted


    For some reason I need to maintain the content of each page after leaving, let's say the user has clicked the counter button a few times and then switched to the fetchdata page, and then switched back to the counter page, the value will reset to 0 because the entire page has been re-rendered. How can I prevent this from happening?

    Kind Regards,

    Tuesday, May 7, 2019 8:22 PM

All replies

  • User753101303 posted


    You have to store this value somewhere so that you can restart from the last value. Where to store a value delpends often on :
    - how long do you need it (ie until the browser session ends, or even if the user comes back 10 days later ?)
    - and/or where you need it (only on the client side, on the server side or both)

    See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2 for possible options (and of course you can store values in a db). Explain what is the purpose of this value if you need help about which option would fit your need.

    The basic principle is that the web is stateless ie when doing an http request (even on the same page) the whole cycle restart from scratch.

    Thursday, May 9, 2019 5:44 PM
  • User787503498 posted

    I can't give you a way to prevent it, I can tell you how I handled it though.

    I set up a service, which is really just a class with an interface.  I store my data in the service.  If I need to store it between sessions, I can store it in a database.

    With client-side Blazor, you can use a singleton service I believe.  And it will be matched only for that session with that user.

    On server-side Blazor, I have to have a unique id because I don't think scoped worked how I thought it would work.  So the data just gets stored alongside other sessions in a List<session> sort of thing.  I don't need it crazy scalable because the max amount of sessions going on at any one time will 10 or 20 because this is an internal app.

    Wednesday, May 29, 2019 5:04 PM
  • User-821857111 posted

    There are a few third party contributions that help to manage state in a Blazor application:

    There is also an open issue in the official repo covering State Management in Blazor: https://github.com/aspnet/AspNetCore/issues/5467

    Friday, May 31, 2019 9:00 AM