开发者

ASP.NET MVC 2 validation: Easy way to add CSS class to element on error?

开发者 https://www.devze.com 2023-01-07 14:13 出处:网络
I\'m using ASP.NET MVC 2 and using Microsoft\'s beautiful new \'DataAnnotations\' to provide some validation hints for my model.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消