Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining viewstate in Asp.net mvc?

One of the major reasons for using webforms is the ease of being able to maintain viewstate. I would like to build an asp.net mvc application so what options do I have for maintaining viewstate?

Kind regards

like image 959
Goober Avatar asked Aug 16 '09 22:08

Goober


People also ask

Can we use Viewstate in ASP.NET MVC?

View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls. You can also use view state to store application data that is specific to a page.

Does MVC maintain viewstate of controls?

ASP.NET MVC will persist the values of the controls long enough for you to validate them and (if needed) to round-trip them back to your page for editing or correction. If the controls validate, you can persist them to a database or other data store, where they will be available for subsequent GET requests.

How do you maintain state in MVC?

In ASP . NET MVC, ViewData, View Bag, TempData is used to maintain the state in our page/view. Viewdata, ViewBag is used to transfer date/information from controller to view in the current request. TempData use to transfer data from controller to another controller.

How will you store value in view state?

View State is one of the methods of the ASP.NET page framework used to preserve and store the page and control values between round trips. It is maintained internally as a hidden field in the form of an encrypted value and a key. Default enables the View State for a page.


2 Answers

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. Once the controller method has been called, what you do with those values is up to you.

ASP.NET MVC will persist the values of the controls long enough for you to validate them and (if needed) to round-trip them back to your page for editing or correction. If the controls validate, you can persist them to a database or other data store, where they will be available for subsequent GET requests.

like image 133
Robert Harvey Avatar answered Oct 06 '22 09:10

Robert Harvey


You can imitate view state by serializing model in view using MVC3Futures project

All you have to do is to serialize model and encrypt it in view.

@Html.Serialize("Transfer", Model, SerializationMode.EncryptedAndSigned)

And in controller add deserialized attribute.

public ActionResult Transfer(string id,[Deserialize(SerializationMode.EncryptedAndSigned)]Transfer transfer)
like image 20
jan salawa Avatar answered Oct 06 '22 11:10

jan salawa