views:

89

answers:

1

Hi. I would like to change the htmlAttributes of the code rendered by my Html.ValidationMessageFor, but I want the message displayed to be the "default".

The overload options are:
A) Html.ValidationMessageFor(expression)
B) Html.ValidationMessageFor(expression, validationMessage)
C) Html.ValidationMessageFor(expression, validationMessage, htmlAttributesObject)

I wish there was a Html.ValidationMessageFor(expression, htmlAttributesObject) option, but there is not.

How can I achieve the desired result with option C? That is, where is that "default" messages stored? Is there an clean, easy way to get at it, so I can plug it in to the validationMessage parameter?

Thanks

A: 

Since you don't want to override the default message, simply supply an empty string and MVC will use the default message:

<%: Html.ValidationMessageFor(m => m.propertyName, string.Empty, new { attribs ... }) %>
Clicktricity
Perfect. Thanks. I guess I assumed passing an empty string would render (or not, as it were) an empty string for the message. Is the fact that empty strings as parameters won't override a helper default some sort of ASP.NET or more general .NET convention? Or is that behavior variable on a case-by-case basis?
Carson Herrick
Neither really. I found out about this functionality by looking at the source code, Its worth looking when you get the chance. Its been done this way because there couldn't be a separate method just taking the two parameters you were originaly wanting to use, without the method signature clashing with (expression, validationMessage).
Clicktricity