开发者

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 w开发者_如何学Gohich 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜