home / software / tips and tricks / How do I get the collection of Model State Errors in ASP.NET MVC?

How do I get the collection of Model State Errors in ASP.NET MVC?

Updated:  03/25/2016 14:03 PM
Author:  Shiju Mathews

Status:    Resolved.


When validation errors are occurring and it is not stopped the form submitting to the "POST" action, we need to debug the errors fro correction. This validation errors can be analyzed by using the "ModelState" object.

Using LINQ version, if you want to join all the error messages into one string as follows

var modelStateErrors = this.ModelState.Values.SelectMany(m => m.Errors);

Or the errors can be loaded into a string as bellow.

string messages = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage));

Tags: How do I get the collection of Model State Errors in ASP.NET MVC?
Updated on: March 2024