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

本文关键字:包含 CheckBox 定义 Web Mvc HtmlHelper System | 更新日期: 2023-09-27 18:37:25

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 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>

但我有同样的错误。知道吗?

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

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