Last post May 31, 2019 09:00 AM by Mikesdotnetting
May 07, 2019 08:22 PM|WDev|LINK
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?
May 09, 2019 05:44 PM|PatriceSc|LINK
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.
May 29, 2019 05:04 PM|bitbrain|LINK
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.
May 31, 2019 09:00 AM|Mikesdotnetting|LINK
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