简体   繁体   中英

How do I save data without using sessions for postback in MVC?

I have 2 texboxes and 1 needs validation. I have this setup with Html.ValidationSummary display. I would like to keep the data that has been entered on the validation post. Instead the page is refreshed and the data is lost which makes the user enter the data again. How do I prevent that without using sessions?

Why don't you follow the pattern outlined in this blog post since postbacks technically don't exist as a concept in ASP.NET MVC:

http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

No session? You need a cookie.

You can either:

Save a unique identifier in the cookie and then use a database to store and retrieve the validation messages.

Store the validation messages in the cookie.

This should be handled out-of-box. When you return the view on validation failure, the values in the ViewModel should be automagically overriden by the values that were originally posted. The ModelBinder stores the input names and their posted values in the ModelState. So, my guess is the ModelState is being misused.

If you use MVC2, you can use MVC Futures Html.Serialize & Deserializing attribute.

Exploring the ASP.NET MVC 2 futures assemby

Check this site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM