【问题标题】:How do I call the following function? [closed]如何调用以下函数? [关闭]
【发布时间】:2013-08-06 15:08:40
【问题描述】:

如何调用这个函数?

public static HtmlString DropdownForEnum<TModel>(this HtmlHelper<TModel> helper, Type type,
    string name, string optionLabel, object htmlAttributes)

【问题讨论】:

  • 多么有用的评论

标签: c# generics


【解决方案1】:

在页面内部(使用 razor 语法):

@Html.DropDownForEnum(typeof(enumToDropDown), name: "Foo", optionLable: "Bar", htmlAttributes: null)

【讨论】:

  • 感谢您给我一个直接的答案,而不是抨击。
【解决方案2】:

参数的“this”部分向我表明这是一个“扩展方法” - 基本上是一个对对象执行一些公共操作的辅助方法,但可以像调用该对象的方法一样调用它。

HtmlHelper<Model> helper;
Type type;
String name;
String optionLabel;
Object htmlAttributes;

helper.DropdownForEnum(type, name, optionLabel, htmlAttributes);
// or, the standard way for calling a static:
NameOfClassWhereYouFoundMethod.DropdownForEnum(helper, type, name, optionLabel, htmlAttributes);

【讨论】:

  • 除了函数有一个静态声明,所以它不会在 HtmlHelper 的实例上调用。
  • @MikeCorcoran 所有扩展方法都是静态声明的,因为 HtmlHelper 的实例被指定为参数。 (这个 HtmlHelper...)
  • 呃,脑残。你说得对,我在想只有外部类声明必须是静态的
【解决方案3】:

这是 HtmlHelper 上的扩展方法。 因此,它应该这样调用:

HtmlHelper<TModel> instance = new HtmlHelper<TModel>();
instance.DropdownForEnum(type, name, optionLabel, htmlAttributes)

其中 TModel 是在声明时分配给泛型的类型。

另请参阅此问题: MVC3 Razor DropDownListFor Enums

关于扩展方法,请看这个: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

【讨论】:

    【解决方案4】:

    这是 HtmlHelper 上的 extension 方法。你可以阅读更多关于它的信息here

    你可以这样称呼它

     yourhtmlHelperObject.DropdownForEnum(someType,someName,label,attributes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2020-11-27
      相关资源
      最近更新 更多