【问题标题】:System.Web.Mvc.HtmlHelper' does not contain a definition for CheckBoxSystem.Web.Mvc.HtmlHelper' 不包含 CheckBox 的定义
【发布时间】:2012-06-17 17:12:44
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
namespace secondMvc.MyControls
{
    public static class CheckBoxList
    {
        public static MvcHtmlString CheckListBox(this HtmlHelper helper, string Name, Dictionary<Int32, string> citiesList, bool IsVertical, string cssClass)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(string.Format("<div >"));
            foreach (var item in citiesList)
            {


                sb.Append(helper.CheckBox(item.Value, true, new { @class = cssClass, value = item.Key }));
                sb.Append(helper.Label("RadioButtonItems", item.Value));
                sb.Append("&nbsp;");
                if (IsVertical) sb.Append("<br>");

            }
            sb.Append("</div> ");
            return MvcHtmlString.Create(sb.ToString());
        }
    }
}

可以找到System.Web.Mvc.HtmlHelper'does not contain a definition forCheckBoxand no extension method'CheckBox'accepting a first argument of type'System.Web.Mvc.HtmlHelper'`(您是否缺少 using 指令或程序集引用?)

我像这样更改 web.config:

<configuration>

    <appSettings>

    </appSettings>

    <connectionStrings>

    </connectionStrings>
  <pages>
    <namespaces>

      <add namespace="secondMvc.MyControls"/>
    </namespaces>
  </pages>

  <system.web>
    <compilation>
      <assemblies>
      <add assembly="secondMvc.MyControls" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

但我有同样的错误。 有什么想法吗?

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    using System.Web.Mvc.Html 添加到包含CheckBoxList 静态类的文件中。正是在这个命名空间中定义了诸如CheckBox 之类的扩展方法。 web.config 命名空间部分在编译 C# 代码时被完全忽略。它们被视图使用。请注意,Razor 视图使用 ~/Views/web.config 文件,而不是 ~/web.config,因此如果您希望自定义扩展方法在视图中得到解析,请确保您已将 secondMvc.MyControls 命名空间添加到正确的 web.config。

    【讨论】:

    • 确保您添加了 secondMvc.MyControls 命名空间是什么意思?我已将 System.Web.Mvc.Html 添加到我的主 Web 配置并查看 web.config 并仍然收到此错误?
    猜你喜欢
    • 2012-09-23
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多