I created an extension method for the HtmlHelper which works very well. Now I need to create the overload that receives an IDictionary so I can add a css class to it so I tried the following:
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
{
return EnumDropDownListFor(htmlHelper, expression, null);
}
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes)
{
var items = DoSomething();
return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
}
When I tried to use it in my view I still got the following exception:
Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.Compiler Error Message: CS1928:
‘System.Web.Mvc.HtmlHelper’ does not
contain a definition for ‘EnumDropDownListFor’ and the best extension
method overload
‘LIMM.Web.HtmlHelpers.HtmlDropDownExtensions.EnumDropDownListFor(System.Web.Mvc.HtmlHelper,
System.Linq.Expressions.Expression>,
System.Collections.Generic.IDictionary)’ has some
invalid arguments
Obviously I’m not extending the method correctly but so google hasn’t been my friend in finding a way to accomplish this. A little help will be appreciated.
Thanks.
UPDATE: As I type the code in the view, intellisense does give me both overloads. The error happens when I run the application.
Maybe you are trying use your helper using the commonest construction (pass html attributes as an anonymous object), so very probably you need an overload like this: