tags:

views:

98

answers:

1

How do I recreate the form prepopulated with posted data when the validation fails and the ValidationSummary is fired off. I get the validators firing but cannot get a way to prepopulate the form.

catch (Exception ex)
{
    TempData["Message"] = "An Error Occured while saving the product!";
    ModelStateDictionary modelDictionary = Validation.UpdateModelStateWithRuleViolation(product, ViewData.ModelState);
    TempData["ModelDataDict"] = modelDictionary;
}

On error, I assign the modelState to a TempData variable which I use but how can I recreate the form with data already posted...

+2  A: 

Make sure that you return the data model which has been found to be invalid so that the form can rebind to the same data as was sent over.

This way you do not lose the content. Of course this will only work if the inputs are already bound the data model. i.e.:

  <label>Name</label>
  <%=Html.TextBox("Name", Model.MyClass.Name)%>
Richard