Get all model validation errors in ASP.NET MVC

December 08, 2014 C# ASP.NET MVC Snippet Validation

In ASP.NET MVC you can get a list of all validation errors for your model from the ModelState:

  if (!ModelState.IsValid) {
    var modelErrors = new List<string>();
    foreach (var modelState in ModelState.Values) {
      foreach (var modelError in modelState.Errors) {
        modelErrors.Add(modelError.ErrorMessage);
      }
    }
    // do something with the error list :)
  }

Related posts: