views:

92

answers:

2

I am newish to MVC and understand all the great things about it, including the reasons why viewstate isn't available, however there are some circumstances where I think having some kind of view state will be quite handy, In my case I am thinking about list pages with various search filters that can be applied to the list.

Would it be worthwhile implementing some kind of pseudo viewstate to hold this info in some cases? or is there a better solution?

Any examples out there?

Your comments appreciated.

+4  A: 

In ASP.NET MVC, state-retention is typically handled by round-tripping the state information back to the view.

For example, in the NerdDinner CRUD examples, they show how you can submit a form, check for errors, and if there are errors, display the form again with the data still intact, including the necessary error messages.

This works because the controller method handling the POST simply passes the data back to the view:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {

    Dinner dinner = dinnerRepository.GetDinner(id);

    try {

        UpdateModel(dinner);

        dinnerRepository.Save();

        // No Errors, return to detail view
        return RedirectToAction("Details", new { id=dinner.DinnerID });
    }
    catch {

        foreach (var issue in dinner.GetRuleViolations()) {
            ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
        }

        // Errors, display form again.  Pass state information back to form.
        return View(dinner);
    }
}
Robert Harvey
+3  A: 

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!

Venemo
[Citation Needed] A hidden input field with garbage values has no known effect on SEO.
jfar
Perhaps. Still, some people don't like having it around. And most of the web controls render garbage html.
Venemo
jfar: Viewstates in WebForms apps can become incredibly large, adding weight to the page size, making your site slower. Google has started to factor in page performance in their rankings, so what Venemo says is valid regarding Viewstate and SEO.
Sunday Ironfoot
@Sunday - Thank you for confirming me.
Venemo