Get all model validation errors in ASP.NET MVC

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:

Comments

comments powered by Disqus