views:

24

answers:

2

I have an ActionResult returning from a strongly typed view where I manually validate some conditions, pass in an error message, but would like to preserve the users responses.

Since my View is strongly typed, I am calling it like this:

return View("PrincipalInvestigatorForm", new SmartFormViewModel(sections, questions));

My problem though, is that the error message is displayed but all the users data is wiped. How do I preserve the "ViewState" in MVC? Is there an easy way?

+1  A: 

What does your action looks like? I'm using something like this:

[HttpPost]
public ActionResult Edit(MyModel model)
{
    if (ViewData.ModelState.IsValid)
    {
        // Whatever...
    }
    else
    {
        return View("Editmodel", model)
    }
}
moi_meme
Thanks, that would work, but there's a problem, I am using a ViewModel, but when I have a ActionResult with my ViewModel as the parameter, I get an error saying I need a parameterless method, even though my view is strongly typed (to my ViewModel).
Mark Kadlec
@Mark Kadlec: Do you have the HttpPost attribute above your action, do you have 2 action for your edit, one for view, one for post?
moi_meme
Just have one Post method and it does have the attribute.I call: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Next(List<Answer> answers) { ... }and this never gets called. Can you not have lists returned?
Mark Kadlec
+1  A: 

Your best bet is to re-populate the SmartFormViewModel model based on the form information.

bryanjonker
Thanks Bryan, I guess that's the option, not that big a deal, I thought there might be a quicker option (silver bullet), but when I think about it, I'd have to have the entire HTML string in memory and Formscollection is more of an array of results.It will at least be easy to parse through the results and the upside is that I now have complete control over what to persist. Thanks.
Mark Kadlec