如何自定义Html.ValidationMessageFor在ASP MVC [英] How to customize Html.ValidationMessageFor in ASP MVC
问题描述
是否可以定制Html.ValidationMessageFor方法,使其产生不同的HTML?
我想要做类似的东西:
< DIV CLASS =字段的出错框>
< DIV CLASS =顶>< / DIV>
< DIV CLASS =中>< P>这字段是必需的< / P>< / DIV>
< / DIV>
我不知道是否有可能使用 P
aragraph,而不是默认的跨度
,因为它可能使无法验证插件放置错误消息。但对于DIV -s,那是很容易 - 你可以编写自定义HTML帮手。
沿着这些东西线(的可能需要进一步的测试/编码的)。您将需要包括这种静态扩展方法的命名空间在您看来,或将其放入 System.Web.Mvc.Html
直接
公共静态类验证
{
公共静态MvcHtmlString MyValidationMessageFor<的TModel,TProperty>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,TProperty>>前pression)
{
TagBuilder containerDivBuilder =新TagBuilder(分区);
containerDivBuilder.AddCssClass(字段错误盒); TagBuilder topDivBuilder =新TagBuilder(分区);
topDivBuilder.AddCssClass(顶部); TagBuilder midDivBuilder =新TagBuilder(分区);
midDivBuilder.AddCssClass(中间);
midDivBuilder.InnerHtml = helper.ValidationMessageFor(如pression)的ToString(); containerDivBuilder.InnerHtml + = topDivBuilder.ToString(TagRenderMode.Normal);
containerDivBuilder.InnerHtml + = midDivBuilder.ToString(TagRenderMode.Normal); 返回MvcHtmlString.Create(containerDivBuilder.ToString(TagRenderMode.Normal));
}
}
如你所见,这将使用默认 ValidationMessageFor
的方法,不使用验证,插件错误消息处理干扰。
和你使用这个简单的,因为默认的验证消息帮手
@ Html.MyValidationMessageFor(型号=> model.SomeRequiredField)
Is it possible to customize the Html.ValidationMessageFor method so that it produces different HTML?
I want to do something similar to:
<div class="field-error-box">
<div class="top"></div>
<div class="mid"><p>This field is required.</p></div>
</div>
I am not sure if it's possible to use p
aragraph instead of default span
, as it may make impossible for validation plugin to place error messages. But for div -s, thats easy - you could write custom html helper.
Something along these lines (may need further testing/coding). You will need to include the namespace of this static extension method in your view, or put this into System.Web.Mvc.Html
directly.
public static class Validator
{
public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
{
TagBuilder containerDivBuilder = new TagBuilder("div");
containerDivBuilder.AddCssClass("field-error-box");
TagBuilder topDivBuilder = new TagBuilder("div");
topDivBuilder.AddCssClass("top");
TagBuilder midDivBuilder = new TagBuilder("div");
midDivBuilder.AddCssClass("mid");
midDivBuilder.InnerHtml = helper.ValidationMessageFor(expression).ToString();
containerDivBuilder.InnerHtml += topDivBuilder.ToString(TagRenderMode.Normal);
containerDivBuilder.InnerHtml += midDivBuilder.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(containerDivBuilder.ToString(TagRenderMode.Normal));
}
}
As you see, this uses default ValidationMessageFor
method, to not interfere with validation-plugin error message processing.
And you use this simply, as default validation message helper
@Html.MyValidationMessageFor(model => model.SomeRequiredField)
这篇关于如何自定义Html.ValidationMessageFor在ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!