4

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?

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For MVC3, using System.Web.Mvc; is the namespace I used for extensions methods for Html.LaborFor System.Web.Mvc.Html is already in the inner web.config file really zero sense in using in when modding a helper, just use using System.Web.Mvc; and setup your web.config to be "aware" of your namespace with the extension method, thus see my answer below.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.