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:

  1.   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 🙂
      }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s