Is there a way to access validation error messages in the controller. I'm not finding them anywhere in ModelState.
3 Answers
Iterating over ModelState is used for this purpose. Something along these lines:
if (!ModelState.IsValid)
{
StringBuilder result = new StringBuilder();
foreach (var item in ModelState)
{
string key = item.Key;
var errors = item.Value.Errors;
foreach (var error in errors)
{
result.Append(key + " " + error.ErrorMessage);
}
}
TempData["Errors"] = result.ToString();
}
Comments
You can validate an object in the controller using the Validator
1 Comment
Faust
I don't need to validate in the controller -- that's already being done by the MVC framework -- I just want to access the error messages generated by the framework's validation process in the controller rather than the view. I want to place the messages into the master view via TempData