Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
Why ASP.NET MVC does not support ViewState? Anyone please explain with a good example.
Thanks in advance
Posted

ViewState was required for webforms controls as they needed a mechanism to store additional data so that the controls could be recreated on postback. As only form elements are included in postbacks, anything on the page that isn't a form value is lost. For example an asp:Label has no html form element so there is nowhere to store the label's text, it's fore colour and so on, so that data went in ViewState, so when you did a postback it would know what the text of the label was and what colour it was.

MVC doesn't support webforms controls, you need to manage all the html rendering and state yourself. ViewState was just a hidden field anyway, and you can still use the same technique for storing data about controls if you need to.
 
Share this answer
 
Quote:
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.

and red this[^] as well.

-KR
 
Share this answer
 
Comments
Parthasarathy Mandayam 17-May-23 6:15am    
Which controller method are they posted to and how do you retrieve those values?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900