I am using ASP.NET MVC 3 and I have a C# object that I need to maintain between requests. It maintains some server-side state information that is not really important to this question. Currently this object is serialized into session and then pulled out upon each request.
The problem with this approach is that the object is specific to session, but I'd like it to be specific to each request. So opening a second tab in your browser would essentially have its own object, rather than sharing the same object as the first tab because it's the same session.
So obviously I need to store this object on the page and pass it back with requests. I'm wondering what the best way to go about this is. The client does not need the information in this object and the data in this object is not sensitive data so I'm not trying to prevent it from being viewed by visitors. I am simply wondering what the best way to store this object between request is. Is there a simple way to serialize a C# object into a view, say like in a hidden field (similar to web forms viewstate) and then grab it back out on each request?
View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.
HTTP is a stateless protocol. Each HTTP request does not know about the previous request. If you are redirecting from one page to other pages, then you have to maintain or persist your data so that you can access it further.
MVC is stateless because HTTP is. There is nothing in HTTP that indicates when a session starts or ends.
The best way is to store the user data is in session. as session is a data dictionary held by server across application against per user session.
ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method.
You could use Html.Serialize from the Futures project. You can get it by Install-Package Mvc3Futures
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With