2012-06-17 42 views
2
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 forand no extension method‘复选框’accepting a first argument of type“System.Web.Mvc.HtmlHelper'`可以找到(是否缺少using指令?或程序集引用)System.Web.Mvc.HtmlHelper“不包含复选框

我改变的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> 

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

回答

12

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

+0

你是什么意思确保你已经添加了第二个MVC.MyControls命名空间?我已经将System.Web.Mvc.Html添加到了我的主Web配置并查看了web.config并仍然出现此错误? – Pomster

相关问题