MrM
MrM

Reputation: 22009

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?

Upvotes: 3

Views: 2143

Answers (4)

takepara
takepara

Reputation: 10443

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

Exploring the ASP.NET MVC 2 futures assemby

Check this site.

Upvotes: 1

dotjoe
dotjoe

Reputation: 26940

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.

Upvotes: 1

amurra
amurra

Reputation: 15411

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

Upvotes: 2

John Farrell
John Farrell

Reputation: 24754

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.

Upvotes: 1

Related Questions