Question

I am trying to build an HtmlHelper extension in ASP.NET MVC RC2. This code worked fine in Preview 5, but does not work anymore in RC2 and I am trying to understand why. Here is the code:

public static string EmptyDropDownList(this HtmlHelper htmlHelper, string name, object htmlAttributes)
{
    return htmlHelper.DropDownList(name, new SelectList(new string[0]), htmlAttributes);
}

The problem is that I am unable to access all the methods on htmlHelper from within the extension method. Thus, htmlHelper.DropDownList cannot be found.

Any suggestions?

Was it helpful?

Solution

You need to include the System.Web.Mvc.Html namespace since most of the HtmlHelper methods are really extensions defined in classes in that namespace.

You can find the RC1 (and, presumably, soon the RC2 source code, too) at www.codeplex.com/aspnet. Click on the source code tab and navigate down to the MVC source code tree.

OTHER TIPS

Two choices:

a. Add to page with "Using" thus with razor view page (mvc 3 and mvc 4) e.g.

    @using UrWeb.Helpers

OR

b. Add to the inner web.config namespaces section e.g.

    <add namespace="UrWeb.Helpers"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top