Ambiguous call to LabelFor

Topics: Troubleshooting, Writing modules
Jun 29, 2011 at 1:49 AM

I'm writing a razor template for a custom module, and I'm trying to put in a regular old boring form, that submits to an external url.

When trying to use the html helpers for a label, @Html.LabelFor, I'm receiveing a compiler error that it's an ambiguous call. I then checked the Orchard source to notice that Orchard redefines all the common html helper methods, including LabelFor.

 

The affected line is:

@Html.LabelFor(q => q.DepartureDate, "Departure Date")

 I've removed the namespacing for my model from the compiler error message to simplify things (Quote is my model class):

Compiler Error Message: CS0121: The call is ambiguous between the following methods or properties: 'Orchard.Mvc.Html.HtmlHelperExtensions.LabelFor<Quote,string>(System.Web.Mvc.HtmlHelper<Quote>, System.Linq.Expressions.Expression<System.Func<Quote,string>>, string)'

and 'System.Web.Mvc.Html.LabelExtensions.LabelFor<Quote,string>(System.Web.Mvc.HtmlHelper<Quote>, System.Linq.Expressions.Expression<System.Func<Quote,string>>, string)'

 

Is there something I'm missing on how to resolve the ambiguity? I'd expect this error since the methods are redefined with the same signatures, so there's obviously something I'm missing.

I've tried adding using statements for "Orchard.Utility.Extensions" to no avail, I've also checked that all projects are referencing the same System.Web.Mvc dll in the orchard directory just in case.

 

 

 

Jun 29, 2011 at 5:28 AM

In either the root web.config or Views/web.config you should check what namespaces get compiled into the view. System.Web.Mvc should probably not be there in this case. Usually you resolve this type of conflict by writing the entire class namespace, but this leaves without the syntactic sugar offered by the extension methods.

Jun 29, 2011 at 5:45 AM

My web.configs (module root and in /views) are unmodified and are identical to all the other Web.config's in Orchard.Modules (just checked it against Orchard.Tags).

I can use the entire namespace:

@System.Web.Mvc.Html.LabelExtensions.LabelFor(Html,q => q.DepartureDate, "Departure Date")

I'm just a little bit at a loss for why Orchard would redefine standard MVC methods.

Coordinator
Jun 29, 2011 at 5:54 AM

It won't be for long. 2.0 drops pretty much all usage of the MVC helpers.

Jun 29, 2011 at 6:53 AM

@mshaw62 usually you want more finer control over those things, which is why Orchard is better off without them. For a CMS is good to have  conventional html renderers than to rely on the bunch provided with the standard MVC

Jun 29, 2011 at 1:36 PM
Edited Jun 29, 2011 at 1:38 PM

I created a work item for this a little while back: http://orchard.codeplex.com/workitem/17919

The simple workaround for now is to force it to use the localized overload, like this: @Html.LabelFor(q => q.DepartureDate, T("Departure Date"))