asp.net c# MVC: How do I live without ViewState?

But of course it can. In fact, the web is stateless. Any thoughts to the contrary are the aberration, in fact.

Web Controls are gone in MVC. There are no events firing on the server side. This is replaced by two different mechanisms–URLs and POSTing form data. Proper use of these will replace your need for the ViewState.

In a conventional ASP.NET web application, you would place a LinkButton on your webpage that would perform function X. ASP.NET would stick lots of ViewState cruft, javascript and other stuff into the webpage so that, when the user clicks on the button and “posts back” to the website (by submitting a form nobody knows existed), ASP.NET reconstructs what happened and determines a particular button event handler must be executed.

In MVC, you construct your link to access a particular route. The route describes what the user wishes to do–/Users/Delinquent/Index (show a list of all delinquent users). The routing system in MVC determines which Controller will handle this route and which method on that controller will execute. Any additional information can be transmitted to the controller method by URL query string values (?Page=5 for the 5th page of delinquents).

In addition to URLs, you can use HTML forms to POST back more complex information (such as a form’s worth of data) or things that won’t fit in a query string, such as a file.

So you “maintain” state via query strings and form POST values. You’ll find that, in fact, there isn’t that much state to maintain in the end. In fact, having to maintain lots of state is a good indication that your design is lacking or that you’re trying to do something that doesn’t fit a website model.

Leave a Comment