【问题标题】:Template for ValidationMessageValidationMessage 模板
【发布时间】:2011-05-06 08:34:51
【问题描述】:

MVC 现在包含许多您可以使用模板覆盖的东西,您可以根据任何规则覆盖任何模型字段的 UI 表示,而且非常棒。但是为什么我不能对验证消息做同样的事情,我想它会给开发人员更多的灵活性和便利性。我知道我可以像这样使用覆盖扩展方法:

public static MvcHtmlString ValidationMessage(this HtmlHelper Html, string Model)
   {
     return System.Web.Mvc.Html.ValidationExtensions.ValidationMessage(Html, Model);
   }

但是只要有了这个,我也应该重写 ValidationMessageFor,它也偏离了 MVC 的 html 代码模板的一般实现。

问题:这是解决此类问题的最佳实践吗?

谢谢!

【问题讨论】:

  • 您是否只是想修改错误的文本?您希望通过覆盖 ValidationMessage 来实现什么?

标签: model-view-controller asp.net-mvc-3


【解决方案1】:

当我需要自定义我的 ValidationMessageFor 元素时,我使用 css 来定位呈现的标签。

我知道的唯一模板是 EditorTemplates 和 DisplayTemplates——它们分别需要“EditorFor”和“DisplayFor”。 ValidationMessageFor 是硬编码的。

我确实找到了一些帖子herehere,它们应该可以帮助您编写自己的方法。

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2017-08-16
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多