Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.ValidationMessageFor Text Color

Probably a simple question, but i cant seem to find the answer.

using MVC 2 i have a series of Html.ValidationFor controls. I want to assign a CSS class to the text and cant seem to do it.

<%= Html.TextBoxFor(Model => Model.Chest, new { @class = "textBoxMeasure" })%>
<%= Html.ValidationMessageFor(Model => Model.Chest) %>

if i try the same method as textboxfor i get errors because it requires a string, when i put a string in it still wont work!

thanks

like image 223
loveforfire33 Avatar asked Dec 08 '11 22:12

loveforfire33


2 Answers

I added a comment to the accepted answer, but I cannot to format it for better view. So, here is my already formatted comment from the accepted response.

I had similar case and I used solution from accepted answer. But I desired to use message from model annotation. I tried this:

@Html.ValidationMessageFor(Model => Model.Chest, null, new { @class = "text-danger" });

and it correctly worked for me. I used MVC4 with bootstrap.

like image 102
kikea Avatar answered Sep 27 '22 20:09

kikea


There's a variant that takes htmlAttributes as the third argument (the second is the message that should be displayed, or you can use null for the default validation message)

Html.ValidationMessageFor(
        Model => Model.Chest, 
        "Please enter a value", 
        new { @class = "redText" })

For more info see http://msdn.microsoft.com/en-us/library/ee721293%28v=vs.98%29.aspx

like image 21
Dennis Traub Avatar answered Sep 27 '22 20:09

Dennis Traub