views:

55

answers:

1

I'm using ASP.NET MVC 2 and using Microsoft's beautiful new 'DataAnnotations' to provide some validation hints for my model.

I can get the validation messages to display... but here's the rub: I'd like to add a CSS class to a specific div when there is a validation error involving a specific field.

I figure I can brute force this by checking to see if the <%: Html.ValidationMessageFor(m=>m.MyModelFieldname) %> is empty in the view code markup ... but the amount of bulk that would add to the markup gives me the willies.

Is there an easier or more elegant way to do this?

+1  A: 

You should be able to tell which fields have validation errors by checking if the associated textbox has a input-validation-error class already applied to it, or checking the error message span exists and has a class of field-validation-error.

Another idea is to create your own custom ValidationMessageFor which outputs the html with the div you need with the appropriate class applied.

James O'Sullivan
Yep -- I'm digging the idea of a custom HTML helper.
Dan Esparza