MVC.net 2 - Modify the HTML received with ValidationMessageFor. Could it be with 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 him to do it like this:
<label id="UserName_validationMessage" class="field-validation-valid">A Validation message</label>
Is there a way to do this, like display and editor templates? Or is there another way to do this globally?
+2
a source to share
2 answers
The code that generates this html is inside ValidateExtensions.cs, in System.Web.Mvc. You can update the code to output a label instead of a range and then recompile that. The code looks like this:
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 can override ValidationExtensions.ValidationSummary (this is HtmlHelper htmlHelper, string message, IDictionary htmlAttributes) and do it this way ...
+3
a source to share