可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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" />
回答2:
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) %>
回答3:
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> ...
回答4:
There is an overload that takes two parameters.
But You can try
<%: Html.LabelFor(model => model.FullName) %>
and in your model
public class ModelClass{ .. .. [DisplayName("Full Name")] public string FullName{
回答5:
The method LabelFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, String)
was introduced on MVC 3.0.
Compare
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.labelextensions(v=vs.98).aspx
with
http://msdn.microsoft.com/en-us/library/system.web.mvc.html.labelextensions.aspx (MVC 2.0).
However when I browse to the page I get the titled exception ('no overload...').
Maybe MVC 3.0 is not activated on your IIS.