开发者

Way to keep state in MVC2 without Session or Query string?

开发者 https://www.devze.com 2023-02-04 05:48 出处:网络
Is there a way to keep information between post backs in MVC2开发者_运维技巧 without utilizing Session variables or the query string?You mean like the view state from .NET Web Forms? Technically there

Is there a way to keep information between post backs in MVC2开发者_运维技巧 without utilizing Session variables or the query string?


You mean like the view state from .NET Web Forms? Technically there is, although it isn't recommended - you're much better off utilizing models and posting the model data to the server and pushing the model back into the view.

This will work well but if you're needing something as stateful as the WebForms ViewState, I would recommend doing your project in WebForms or use the session to save your models.

Edit: Build your form that posts (or gets) data back to the same page. Then in your controller, have a method like this.

[HttpPost]
public ActionResult LoginUser(LoginViewModel model)
{
    //work on the model here
    return View(model);
}

This will push the form data that the user just submitted back into your view. Then have an Html helper like this in your view.

<%: Html.TextboxFor(m => Model.Username) %>

There are a slew of excellent resources on the web about using html helpers with models. Google around and you'll come across them.


You could use Hidden Form fields to POST the values back to the server with each form submission.


Other alternatives include a cookie or Http Cache - what is stopping you using session?


As a high level concept, you should rely as little as possible not only on Session to store your state, but on statefulness in general in a web application. The idea is that the web itself is stateless by design, and when designing software on that paradigm the software should be designed to embrace a stateless nature.

More specifically, using a ViewModel gives you a strongly typed representation of the data needed for your view to pass down to the client. Pieces of that data which hold information about the state of a given request which can be made from that view can be added to the view in probably a number of ways, but the two most immediate ones are:

  1. As hidden form field elements
  2. As parts of URLs for requests

Check out the NerdDinner tutorial for a standard approach to using either ViewData or a strongly-typed ViewModel. Some Google searches will, as always, yield more information and tutorials. But note specifically where the tutorial uses the ViewModel properties in the view. These can be used anywhere in the rendering of the HTML, either in the HTML helpers or in manually constructing the tags.

Additional interesting reading regarding the statelessness of the web (and this whole not-as-new-as-many-people-seem-to-think REST thing) is the article: How I Explained REST to My Wife.


If your main issue with the Session variables is of a practical nature (want something that works for a single request, not needing to worry about cleaning it up etc) rather than a requirement to not use Session then use the TempData dictionary. It deals with putting information in the Session for a single request only and the framework will automatically remove it for you afterwards.

0

精彩评论

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