I have an action method as below :
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
if (ModelState.IsValid)
{
//Do Something
}
var model1 = new IndexViewModel();
ModelState.AddModelError("", "An error occurred while editing the user.");
return RedirectToAction("Index", model1);
}
On validation error, i want it to be transferred to the below method with the Model State error.
[HttpGet]
public ActionResult Index(IndexViewModel model)
{
IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
}
My index.cshtml has a validation summary defined to display the model state error.
<div class="row">
<div class="col-xs-12">
@Html.ValidationSummary("", new { @class = "alert alert-danger validation" })
</div>
</div>
Is there a way i can pass the model state error from the Edit method to the index method and load it on load of index screen? The current code is not working. The allErrors field is empty and does not contain any of the added errors.