ElHaix
ElHaix

Reputation: 12986

How to fix ambiguous calls when defining helper methods in MVC4?

Following this example, I created a helper method in App_Code/HtmlHelpers.cs:

namespace AdminWebsite.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper,
                                             string text, string action,
                                             string controller,
                                             object routeValues = null,
                                             object htmlAttributes = null)
        {
            var li = new TagBuilder("li");
            ...

I added the namespace in ~/Views/Web.confg, and @using AdminWebsite.Helpers; at the top of _Layout.cshtml.

Usage:

@Html.MenuItem("Home", "Home", "Home")

I am at a loss as to how it is ambiguous to itself:

The call is ambiguous between the following methods or properties: 'AdminWebsite.Helpers.HtmlHelpers.MenuItem(System.Web.Mvc.HtmlHelper, string, string, string, object, object)' and 'AdminWebsite.Helpers.HtmlHelpers.MenuItem(System.Web.Mvc.HtmlHelper, string, string, string, object, object)

Upvotes: 0

Views: 899

Answers (2)

TimMc
TimMc

Reputation: 21

It's taken me 2 weeks to find out how to REALLY fix this issue. I had put a new class file under the App_Code folder (I haven't used that folder for ages). For some reason I had set the "Build Action" to "Compile". Well, I guess anything under the App_Code folder is already compiled by default, so when the project would build, it would give me this "ambiguous" error. By simply setting the "Build Action" back to "None", the ambiguous error went away! There's a tip for all you putting your helper methods under the "App_Code" folder!

Upvotes: 2

ElHaix
ElHaix

Reputation: 12986

It appears that something was wrong with either one of the web.config's. Simply took the web.config's from a blank MVC4 project and replaced.

Incidentally, having the namespace in both the config and layout does not throw an error.

Upvotes: 0

Related Questions