Search code examples
asp.net-mvc-3shopping-carthiddenambiguityambiguous-call

ASP.NET MVC error: The call is ambiguous between the following methods or properties


I have a problem. In my View of a product I have a button to add it to cart which looks like this:

<div>
<% using(Html.BeginForm("AddToCart", "Cart")) {%>
    <%: Html.HiddenFor(x => x.id_produktu) %>
    <%: Html.Hidden("returnUrl", Request.Url.PathAndQuery) %>
    <input type="submit" value="Dodaj do koszyka" />
    <% } %>
    <h4><%: Model.cena_produktu.ToString("c")%></h4>

For this line:

<%: Html.Hidden("returnUrl", Request.Url.PathAndQuery) %>

I get an error:

The call is ambiguous between the following methods or properties: 'System.Web.Mvc.TextInputExtensions.Hidden(System.Web.Mvc.HtmlHelper, string, object)' and 'System.Web.Mvc.Html.InputExtensions.Hidden(System.Web.Mvc.HtmlHelper, string, object)'

How to solve this? Thank you in advance.


Solution

  • Three ways:

    1. Fully qualify the method:

      System.Web.Mvc.Html.Hidden(Html, "returnUrl", Request.Url.PathAndQuery)
      
    2. Make your own static method with a different name that obfuscates the name.

      public static string TheHiddenIWant(this HtmlHelper helper, string name, object value)
      {
          return System.Web.Mvc.Html.Hidden(helper, name, value);
      }
      Html.TheHiddenIWant("returnUrl", Request.Url.PathAndQuery);
      
    3. Don't include the reference or using statement for the extension method you don't want. For example, get rid of using System.Web.Mvc.TextInputExtensions, or just get rid of the reference.