Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ViewState relevant in ASP.NET MVC?

I am learning ASP.NET MVC and many concepts of "ASP.NET" like authentication, authorization, session state still apply to it.

But from whatever little I have understood, I don't see that ViewState is still relevant in an ASP.NET application. But it is there (property of System.Web.Mvc.ViewPage)! Is it there only for compatibility reasons or it still have some purpose/use?

like image 622
Hemant Avatar asked Jul 23 '09 09:07

Hemant


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.

What is ViewState in ASP.NET MVC?

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.

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.

Does ViewState affect performance?

To accomplish our tasks, we use ViewState a lot, but as we know, it doesn't come free - it has a performance overhead. The ViewState is stored on the page in the form of a hidden variable. Its always advised to use the ViewState as little as possible. We also have other ways to reduce the performance overhead.


2 Answers

Yes, that is correct. ViewState is not relevant. More on differencies between Page Model and MVC here:

Compatibility of ASP.NET Web Forms and ASP.NET MVC

like image 116
Dzmitry Huba Avatar answered Sep 22 '22 12:09

Dzmitry Huba


Its present because ViewPage inherits from Page. However Page itself had no use for ViewState its used by WebControls. It is possible to include original WebControls in a View but doing so would be completely missing the point of separating control from view.

like image 24
AnthonyWJones Avatar answered Sep 23 '22 12:09

AnthonyWJones