MVC.net 2 - change the HTML outputed by ValidationMessageFor - can this be down via templates?
MVC.net 2 by default outputs validation messages like this:
<span id="UserName_validationMessage" class="field-validation-valid">A Validation message</span>
I would like it to do it like this:
<label id="UserName_validationMessage" class="field-validation-valid">A Validation message</label>
Is there a way to do it like the display and editor templates? Or is there another way to do it globa开发者_如何学JAVAlly?
The code that generates that html is inside of ValidateExtensions.cs, in System.Web.Mvc. You could update the code to output a label instead of a span and then recompile that. The code looks like the following:
TagBuilder builder = new TagBuilder("span");
builder.MergeAttributes(htmlAttributes);
builder.MergeAttribute("class", HtmlHelper.ValidationMessageCssClassName);
builder.SetInnerText(String.IsNullOrEmpty(validationMessage) ? GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState) : validationMessage);
Otherwise, you might be able to override ValidationExtensions.ValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary htmlAttributes) and do it that way...
I think this is exactly what you're looking for:
http://blog.syntaxc4.net/?p=711
精彩评论