2012-06-17 33 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

相關問題