Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store an object in the view for state persistence in MVC 3?

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?

like image 748
Chev Avatar asked Feb 15 '12 16:02

Chev


People also ask

How view state is maintained in MVC?

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.

How to maintain state in ASP NET MVC?

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.

Is MVC stateless or stateful?

MVC is stateless because HTTP is. There is nothing in HTTP that indicates when a session starts or ends.

What are different ways to maintain user state between multiple requests?

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.

Does MVC supports ViewState?

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.


1 Answers

You could use Html.Serialize from the Futures project. You can get it by Install-Package Mvc3Futures

like image 70
Otávio Décio Avatar answered Oct 05 '22 12:10

Otávio Décio