Steve Fenton

Squash ASP.NET MVC ModelState Errors into a String or JSON

Once ASP.NET MVC has done all of the hard work of validating a model for you, you may find yourself wanting to squash the ModelState values into a string or JSON object. Because of the hierarchy, it might not be obvious at first glance how to do this, so your best bet would be to write it down on your blog so you can remember:

string errors = JsonConvert.SerializeObject(ModelState.Values
    .SelectMany(state => state.Errors)
    .Select(error => error.ErrorMessage));