How do I find out the error count in a ASP.NET MVC View?
I assume you mean from the view. The following is untested.
ViewData.ModelState.Values.Where( v => v.Errors.Count != 0 ).Count()
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.