【问题标题】:MVC 3 "does not contain a definition for validationmessagefor"MVC 3“不包含验证消息的定义”
【发布时间】:2012-08-17 15:48:55
【问题描述】:

我有以下代码尝试使用我自己的 @Html.MyValidationMessageFor(model => model.myfield) 扩展 @Html.ValidationMessageFor 并提供自定义错误 html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Linq.Expressions;

namespace Hml.BackEnd.Helpers
{
public static class HtmlHelpers
{
    public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
    {
        string myContent = "<div>" + htmlHelper.ValidationMessageFor(expression).ToString() + "</div>";
        return MvcHtmlString.Create(myContent);
    }
}
}

我收到以下编译消息:

'System.Web.Mvc.HtmlHelper 不包含 ValidationMessageFor 的定义,并且没有扩展方法 ValidationMessageFor 接受类型为“System.Web.Mvc.HtmlHelper”的第一个参数,如果您缺少使用或组装参考。

我无法理解,因为 System.Web.Mvc 在项目引用和使用中。

【问题讨论】:

  • ValidationMessageFor 需要 System.Web.Mvc.Html
  • 谢谢你的工作——我为此挠了一阵子!

标签: asp.net-mvc validation html-helper


【解决方案1】:

ValidationExtensions.ValidationMessageForSystem.Web.Mvc.Html 命名空间的一部分。您需要使用完整的命名空间显式引用ValidationExtensions,或者在using 指令中包含System.Web.Mvc.Html

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多