Categories
Uncategorized

How to read all errors from ModelState in ASP.NET MVC

ModelState plays an important role in client-side validation with ASP.NET MVC. Today I wanted to validate my model on server side and prepare a list of errors based on this validation.

I was able to do this using a little LINQ,

public static List<string> GetErrorListFromModelState(ModelStateDictionary modelState)  
{
var query = from state in modelState.Values
from error in state.Errors
select error.ErrorMessage;
var errorList = query.ToList();  
return errorList;
}
The above method returns a list of validation errors. Hope this helps :)
Further Reading :
How to get all Errors from asp.net mvc modelState?
How do I get the collection of Model State Errors in ASP.NET MVC?