简体   繁体   English

如何使用IDictionary扩展HtmlHelper <string, object> 超载?

[英]How to extend the HtmlHelper with the IDictionary<string, object> overload?

I created an extension method for the HtmlHelper which works very well. 我为HtmlHelper创建了一个扩展方法,该方法很好用。 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: 现在,我需要创建一个接收IDictionary的重载,以便可以向其中添加一个CSS类,因此我尝试了以下操作:

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 编译器错误消息:CS1928:'System.Web.Mvc.HtmlHelper'不包含'EnumDropDownListFor'的定义,并且最佳扩展方法重载'LIMM.Web.HtmlHelpers.HtmlDropDownExtensions.EnumDropDownListFor(System.Web.Mvc.HtmlHelper,System .Linq.Expressions.Expression>,System.Collections.Generic.IDictionary)'具有一些无效的参数

Obviously I'm not extending the method correctly but so google hasn't been my friend in finding a way to accomplish this. 显然,我没有正确扩展该方法,但是google并不是我的朋友,无法找到一种方法来实现此目的。 A little help will be appreciated. 一点帮助将不胜感激。

Thanks. 谢谢。

UPDATE : As I type the code in the view, intellisense does give me both overloads. 更新 :在视图中键入代码时,intellisense确实给了我两个重载。 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: 也许您正在尝试使用最常用的构造(使用html属性作为匿名对象)来使用您的辅助程序,因此很有可能需要这样的重载:

public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes)
{
    return EnumDropDownListFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何投放IDictionary <string, object> 到IDictionary <string, MyType> - How to cast IDictionary<string, object> to IDictionary<string, MyType> 如何声明一个类型的(字符串,IDictionary<string, object> ) - How to declare a type of (string, IDictionary<string, object>) 如何转换IDictionary <object,string> 在Unity中解析? - How to convert IDictionary<object,string> to ParseObject in Unity? 如何在 IDictionary 中查找特定值<string, object> ? - How to find a specific value in an IDictionary<string, object>? 身份证<string, object>到 npgsqlParmeterCollection? - IDictionary<string, object> to an npgsqlParmeterCollection? IDictionary 之间的调用不明确<string, object>和字典</string,> - The call is ambiguous between IDictionary<string, object> and IDictionary 如何重载MVC HtmlHelper扩展方法 - How to Overload MVC HtmlHelper Extension Methods IEnumerable的 <IDictionary<string, object> &gt;到IQueryable - IEnumerable<IDictionary<string, object>> to IQueryable 转换IDictionary的优雅方式是什么? <string, object> 到IDictionary <string, string> - What is an elegant way to convert IDictionary<string, object> to IDictionary<string, string> 如何将以下 JSON 数组转换为 IDictionary<string, object> ? - How to convert the following JSON array into IDictionary<string, object>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM