How can I pass back the validation errors I find when using DbEntityValidationException
try
{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
this.ModelState.AddModelError(validationError.PropertyName,
validationError.ErrorMessage);
}
}
return RedirectToAction("AccessDetail", "Home", new { IDValue = access.ID });
}
It appears that when I do this RedirectToAction
my ModelState
refreshes and I can not view the errors it found.
The AccessDetail populates a view model that has many different sources of data in it. So passing just the access to the View does not work.
I was looking at this question but it didn't fit my needs as my view is populated with a ViewModel
RedirectToAction
helper method issues a 302
response to the client which makes the clients to issue a new GET request to the new url.
If you want to persist some data between these 2 requests, Use TempData
.
TempData["Errors"] = yourListOfErrors;
return RedirectToAction("AccessDetail", "Home", new { IDValue = access.ID });
And in your GET
action, read the TempData values and display it.
But If you do not want to do the RedirecToAction, You may simply return the viewmodel back to the view and if you have the ValidationSummary helper method, it will show the validation error messages.
simply use
return View(model);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.