The theoretical answer
ViewState is one of the concepts of ASP.NET WebForms which is considered VERY harmful. It is used to store the state of some controls and renders a very-VERY ugly hidden field into the HTML.
This is undesirable for some people for SEO (search engine optimization) and other reasons.
MVC doesn't provide some of the abstractions that WebForms does, for some very basic reasons:
- It gives you full control over your URLs and output HTML
- WebForms controls (especially the "advanced" ones) render junk HTML, MVC lets you write your own HTML
- In order to avoid these faults, MVC doesn't use the principle of controls, events, and such stuff
- If you wanted your web framework to abstract away the REAL way HTTP behaves, you should continue to use WebForms
In truth, HTTP is a stateless protocol, which WebForms try to hide from you by introducing the concept of Controls, and their state and events.
MVC doesn't lie to you and doesn't try to hide anything from you.
The practical anwser
You can use ViewData
instead of ViewState
.
You set an item (ViewData["Something"] = yourObject
) in the Controller
, and then you can retrieve it in the View
. You can use it to "remember" whatever you want it to.
So, basically, persisting the information consists of reading it from Request.QueryString
or Request.Form
in the appropriate Controller
action, setting it into the ViewData
, and then retrieving the ViewData
information in the View
.
For example:
Controller action:
string textBoxValue = Request.Form["myTextBox"];
...
ViewData["myTextBox"] = textBoxValue;
View:
<% using (Html.BeginForm()) { %>
<%= Html.TextBox("myTextBox") %>
<% } %>
More stuff
Read some MVC-related questions here (such as this one), and read the MVC book (at least the free NerdDinner chapter).
MVC will be much more understandable for you then, I promise!