How to retrieve all the Errors from the ModelState in ASP.NET MVC ?

Published on February 20, 2013 by abundantcode

Filed under ASP.NET

Last modified February 20, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 4384 times

To display all the possible error messages from the ModelState in ASP.NET MVC , the developers would use the Html.ValidationSummary helper method in the View . How to get the list of all the Errors from the ModelState in ASP.NET MVC Controller ?

The ModelState.Values property is a ICollection which contains the list of all the states which the developers can use to filter out the error states as shown in the below sample code snippet.

How to retrieve all the Errors from the ModelState in ASP.NET MVC ?

public ActionResult Index()

{

var states = ModelState.Values;

// Error States

var errorsStates = from m in states

select m.Errors;

return View();

}
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

How to set default value for Html.TextBoxFor in ASP.NET MVC ?

You can set the default value for the Html.TextBoxFor extension controls in ASP.NET MVC by specifyin...

ASP.NET MVC and View State

The ASP.NET Web Forms includes the Server Controls which generally retains the state of the controls...

How to return Http Status Code from ASP.NET Web API Controller ?

There are times when you might want to return the Http Status code from an action method of a ASP.NE...