views:

109

answers:

2

I want to format the title of my ValidationSummary using a string something like:

"There are {0} errors on this page."

How do I find out the number of errors without doing it in the controller and adding it to ViewData?

+2  A: 

If you are referring to the ASP.NET MVC 1.0 version of IEnumerable<RuleViolation>, you can get the count this way:

var errorCount = GetRuleViolations().Count();

To get that count into the view without putting it into view data, you can, you can create an overload for the ValidationSummary HtmlHelper extension method that returns text which includes the error count. This gives you access to the error count from within the extension method.

To see the code in the original ValidationSummary extension method, you can use Reflector to decompile it, or download the ASP.NET MVC source from Codeplex.

Note that the validation mechanism has changed substantially in ASP.NET MVC 2.0.

Robert Harvey
Thanks, but it's MVC 2.0
Moose Factory
+4  A: 

I assume you mean from the view. The following is untested.

ViewData.ModelState.Values.Where( v => v.Errors.Count != 0 ).Count()
tvanfosson
Consider it tested. It works a treat, thanks.
Moose Factory