System.Web.mvc.HtmlHelper does not contain a definition for EnumDropDownListFor

As @ArhiChief hinted at in their answer, my problem was:

using System.Web.WebPages.Html;

Instead of:

using System.Web.Mvc;

Both of which have definitions for HtmlHelper so if you add the wrong one, you will get this error. Replacing it with the correct namespace will fix it.


You forgot to bring the extension method in scope inside the view. This EnumDropDownListFor method is defined in some static class inside a namespace, right?

namespace AppName.SomeNamespace
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ...
        }
    }
}

You need to add this namespace inside the view in which you want to use this helper:

@using AppName.SomeNamespace
@model MyViewModel
...
@Html.EnumDropDownListFor(model => model.Code.Codes)

And to avoid adding this using clause to all your Razor views you could also add it to the <namespaces> section of your ~/Views/web.config file:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="AppName.SomeNamespace" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

Tags:

Asp.Net Mvc