本文介绍了MVC.net 2 - 更改通过ValidationMessageFor输出的HTML - 可以通过模板下载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,MVC.net 2会输出如下验证消息:

 < span id =UserName_validationMessageclass =字段验证有效>验证消息< / span> 

我希望这样做:

 < label id =UserName_validationMessageclass =field-validation-valid>验证消息< / label> 

有没有办法像显示和编辑器模板那样做?还是有另一种方法可以做到全球?

解决方案

生成该html的代码位于System.Web.Mvc中的ValidateExtensions.cs中。您可以更新代码以输出标签而不是跨度,然后重新编译该标签。代码如下所示:

  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);

否则,您可能可以重写ValidationExtensions.ValidationSummary(this HtmlHelper htmlHelper,string message,IDictionary htmlAttributes )并且这样做......


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 globally?

解决方案

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...

这篇关于MVC.net 2 - 更改通过ValidationMessageFor输出的HTML - 可以通过模板下载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:53