System.Web.Mvc.HtmlHelper'不包含CheckBox的定义

Mah*_*ine 2 c# asp.net-mvc-3

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

System.Web.Mvc.HtmlHelper" does not contain a definition for复选框and no extension method'复选框’ 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>
Run Code Online (Sandbox Code Playgroud)

但我有同样的错误.任何的想法?

Dar*_*rov 12

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