Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation messages lost in POST-Redirect-GET in MVC
    text
    copied!<p>I have a ProductController with actions Index (which Loads a blank form). The form also posts to itself as its a complex form and the form elements like dropdowns show posted values the code is as follows</p> <pre><code> public ActionResult Index() { int id; id = Convert.ToInt32(Request.Form["ddlLendingType"]); if (id == 0) id = 1; ProductCommonViewModel viewData = new ProductCommonViewModel(_prodRepository.Method1(),_prodRepository.Method2()) return View(viewData); } </code></pre> <p>When I click submit from the form, it saves the product and if it fails it should show the validation error messages.</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Save(FormCollection fc) { Product product = new Product(); try { ...fill out all properties from form collection _prodRepository.SaveProduct(product); return RedirectToAction("Index", "Product"); } catch (Exception ex) { TempData["Message"] = "An Error Occured while saving the product!"; Validation.UpdateModelStateWithRuleViolation(product, ViewData.ModelState); // WHEN I call redirect to action Index on this view I can see the TempData variable but I cannot see validation summary and individual validation messages.How do I persist the msgs across requests? } } </code></pre> <p>The helper method definition is as follows:</p> <pre><code>public static void UpdateModelStateWithRuleViolation(IRuleEntity entity, ModelStateDictionary dictModel) { List&lt;RuleViolation&gt; violations = entity.GetRuleViolations(); foreach (var item in violations) { dictModel.AddModelError(item.PropertyName, item.ErrorMessage); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload