开发者

ASP.NET MVC: How to create a basic Pseudo Viewstate? Or better solution?

开发者 https://www.devze.com 2022-12-29 12:56 出处:网络
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

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开发者_Go百科?

Any examples out there?

Your comments appreciated.


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);
    }
}


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号