Matt
Matt

Reputation: 6943

MVC3: Razor Validation Text: Make red without setting validation message to null

This works fine:

@Html.ValidationMessageFor(m => m.Definition)

except for the fact that the text comes out black. I want it in red. So, I tried this:

@Html.ValidationMessageFor(m => m.Definition, null, new { style = "color: red" })

However, that is setting the message to NULL. It's crazy that there is no overload that will take the htmlAttributes arg without the message arg..... argghhh!!!

What's the most elegant way to accomplish this (without me having to write my own validation messages)?

Upvotes: 2

Views: 10704

Answers (1)

Matt
Matt

Reputation: 6943

Found the answer here: http://www.jstawski.com/archive/2011/05/12/asp.net-mvc3-change-validation-class.aspx

EDIT

Answer now here: http://jstawski.com/post/2011/05/13/ASPNET-MVC3-change-validation-class

Basically:

If you are using the ASP.net MVC3 built in validation such as

@Html.ValidationMessageFor(m => m.Username)

you can easily change the validation by overriding the CSS class field-validation-error like this:

.field-validation-error
{
    color: red;
}

.validation-summary-errors
{
    color: Red;
}

Upvotes: 10

Related Questions