Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Mvc.HtmlHelper' does not contain a definition for CheckBox

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'` could be found (are you missing a using directive or an assembly reference?)

i change web.config like this:

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

but i have same error. any idea?

like image 795
Mahdi_Nine Avatar asked Oct 20 '25 09:10

Mahdi_Nine


1 Answers

Add using System.Web.Mvc.Html to your file containing the CheckBoxList static class. It is inside this namespace that extension method such as CheckBox are defined. The web.config namespaces section is completely ignored when compiling C# code. They are used by views. And note that Razor views use the ~/Views/web.config file, not ~/web.config, so make sure you have added the secondMvc.MyControls namespace to the correct web.config if you want your custom extension method to be resolved in views.

like image 183
Darin Dimitrov Avatar answered Oct 21 '25 22:10

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!