How to get all Errors from ASP.Net MVC modelState?

chobo2 picture chobo2 · Aug 30, 2009 · Viewed 327.6k times · Source

I want to get all the error messages out of the modelState without knowing the key values. Looping through to grab all the error messages that the ModelState contains.

How can I do this?

Answer

mmutilva picture mmutilva · Feb 8, 2011

Using LINQ:

IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);