Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No overload for method 'LabelFor' takes 2 arguments" in MVC3

I'm running ASP.NET MVC 3 and I'm looking at a Edit view for my model. I've got a FullName property which I want to render as "Full name".

Here's the offending line(s):

<div class="display-label">
    <%: Html.LabelFor(model => model.FullName, "Full name") %>
</div>

Now the intellisense shows that the overload exists - there are two signatures, the first taking just the Expression and the second taking both the Expression and the string to be displayed. However when I browse to the page I get the titled exception ('no overload...').

Anyone have any success using this overload, and any advice on what I might be missing?


Update: I've tried reinstalling MVC3 to no avail. However I have noticed this in the compiler output on the error page:

c:\Windows\assembly\GAC_MSIL\System.Web.Mvc\2.0.0.0__31bf3856ad364e35\
    System.Web.Mvc.dll: (Location of symbol related to previous error)

Which indicates to me that I'm using MVC 2, not MVC 3.

What have I misconfigured, or why would my IIS be using MVC 2 rather than MVC 3? How can I fix this?

like image 514
Kirk Broadhurst Avatar asked Feb 27 '11 02:02

Kirk Broadhurst


3 Answers

You should check your web config if this was an MVC 2 project at one point.

You might be referencing the correct version of the DLL in the project References but pulling in 2.0.0.0 in the web config when it runs...?

http://blog.devlpr.net/2010/07/27/upgrading-an-asp-net-mvc-2-project-to-asp-net-mvc-3/

In my MVC3 app:

<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
like image 149
Perry Avatar answered Oct 22 '22 20:10

Perry


Instead of doing that, decorate the field with the [DisplayName] attribute:

[DisplayName("Full name")]
public string FullName { get; set; }

Then you can use the regular overload:

<%: Html.LabelFor(model => model.FullName) %>
like image 4
RPM1984 Avatar answered Oct 22 '22 18:10

RPM1984


You need to make sure you reference MVC 3.0 in your project. Since this assembly has a strong name, you should be getting it or fail.

For this, in Visual Studio, check Solution Explorer, [Project], References, click on System.Web.Mvc, and check the 'Version' property in the property grid. It should be 3.x.

If you have that, than check the web.config or the machine.config and make sure there is no forced redirection on MVC 2.x.

For this, in all Web.config files in the project, globally and replace the MVC version (replace this System.Web.Mvc, Version=2.0.0.0 by this System.Web.Mvc, Version=3.0.0.0).

Eventually, you can also force redirection from 2 to 3 using this snippet in the root web.config:

...
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
      <bindingRedirect oldVersion="2.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>
...
like image 2
Simon Mourier Avatar answered Oct 22 '22 20:10

Simon Mourier