No overload for shortcut method takes 3 arguments

I am using ASP.Net MVC 3, referring to all the relevant DLLs (see screenshots), but for some reason I am getting this compilation error.

Method signature

Web.config assemblies

Compilation Error

+4
source share
5 answers

It was a strange phenomenon. I was able to reproduce your error. I also managed to find out why this is happening and how to get around it.

Why is this happening

There is a collision between the System.Web.Mvc.Html and System.Web.WebPages.Html

As a result of a collision

System.Web.Mvc.Html

This namespace contains a method definition

LabelExtensions.Label (HtmlHelper, String, String)

As you noticed, this is a namespace that has a use case and causes a problem. It has only two overloads. (String) or (String, String) . During testing, this was the only option that appeared.


How to get around it

What you really wanted is a namespace

System.Web.WebPages.Html

This namespace contains a method definition

HtmlHelper.Label (String, String, IDictionary<String, Object>)

Now you can see that it is possible to add new { @class = "myClass" } .

Create your own instance

Get your own HtmlHelper and LabelExtension as follows:

Razor

 @{ var h = new System.Web.WebPages.Html.HtmlHelper(); } 

asp

 <% var h = new System.Web.WebPages.Html.HtmlHelper(); %> 

Make sure that it is not nested inside the razor block or <%= , because this will prevent its availability. It must be at the global page level.

Use correct label

Now you can use this:

Razor

@h.Label("firstName", "First Name", new { @class = "control-label" })

asp

<%= h.Label("firstName", "First Name", new { @class = "control-label" }) %>

+7
source

It seems to me that you chose overloading from Asp.Net Mvc 4, and then the compiler complains because it does not exist in Asp.Net Mvc 3.

Check these links to see overloads of Html.Label(...) in different versions of Asp.Net Mvc:

Asp.Net Mvc 2
Asp.Net Mvc 3
Asp.Net Mvc 4

Based on the information on the linked pages, it seems that your Visual Studio (for some reason) gives you Intellisense based on Asp.Net Mvc 4, but when it is assumed that the page will be compiled, the version of Mvc version 3 is used and the overload you selected is not exist.

Not sure why VS gives Intellisense from version 4 though ...

+1
source

Look at ~/Views/Web.config

See if there is a configuration/system.web/pages/namespaces node

Try adding <add namespace="System.Web.Mvc.Html"/> there.

The Label method is in the above namespace. Your opinion does not seem to find it.

Since you are not compiling views, it is just fine. Also, your intellisense gets the method just different from the links. However, it is almost as if your view did not get the method at runtime.

Adding the above namespace to the web.config file MAY fix the problem, it hides a potentially big problem in your application. A “fix” should provide a starting point for further debugging the situation.

If this fixes your problem, try comparing your web.config files with a completely new and untouched MVC project. See how you did it differently, which might prevent your views from finding the Html Helper.

0
source

You get this exception because you are trying to use HtmlExtension , which does not exist in ASP.NET MVC3 by default. You need to add support for the HTML attribute in Html.Label() .

Basically you need to add your own extension methods for HtmlHelper . You can start with the source code MVC3. Add this class:

 public static class MyLabelExtensions { public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, object htmlAttributes) { return Label(html, expression, labelText, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, IDictionary<string, object> htmlAttributes) { return LabelHelper(html, ModelMetadata.FromStringExpression(expression, html.ViewData), expression, labelText, htmlAttributes); } // added htmlAttributes to the default MVC implementation internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null) { string str = labelText; if (str == null) { string displayName = metadata.DisplayName; if (displayName == null) { string propertyName = metadata.PropertyName; if (propertyName == null) str = Enumerable.Last( htmlFieldName.Split( new[] { '.' })); else str = propertyName; } else str = displayName; } string innerText = str; if (string.IsNullOrEmpty(innerText)) return MvcHtmlString.Empty; TagBuilder tagBuilder = new TagBuilder("label"); tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId( html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName( htmlFieldName))); // this differs from MVC3 source code: tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.SetInnerText(innerText); // this is different too: return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)); } } 

After you add these extension methods, you can use them in your views as follows:

 <%= Html.Label("firstName", "First Name", new { @class = "control-label" }) %> 
0
source

Instead of using

  <%=Html.Label("firstname","First Name",new {@class="control-label"})%> 

try using the following:

IN RAZOR:

  @Html.Label( "firstname", "First Name" ,new { @class = "control-label" }); 

But I personally would recommend you not to do this. If you have a model and you take the first name from it, you can use it as shown below:

  @Html.Label( model=>model.FirstName, "First Name" ,new { @class = "control-label" }); 

VIEWING IN ASPX:

  <%=Html.Label(model=>model.FirstName,"First Name",new {@class="control-label"})%> 

Remember to add your current model to the watch page, as shown below:

  <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Project.Models.Whatever>" %> 
-1
source

All Articles