-1

How do I call this function?

public static HtmlString DropdownForEnum<TModel>(this HtmlHelper<TModel> helper, Type type,
    string name, string optionLabel, object htmlAttributes)
bman
  • 3,740
  • 9
  • 34
  • 40

4 Answers4

5

Inside a page (using the razor syntax):

@Html.DropDownForEnum(typeof(enumToDropDown), name: "Foo", optionLable: "Bar", htmlAttributes: null)
Mgetz
  • 5,108
  • 2
  • 33
  • 51
2

The "this" part of the arguments indicates to me that that's an "extension method" - basically a helper method that does some public operations on an object, but can be called as though it's a method of that object.

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);
Katana314
  • 8,429
  • 2
  • 28
  • 36
  • except that the function has a static declaration, so it wouldn't be called on an instance of HtmlHelper. – Mike Corcoran Aug 06 '13 at 15:14
  • @MikeCorcoran All extension methods are declared statically, because the instance of HtmlHelper is specified as an argument. (this HtmlHelper...) – Katana314 Aug 06 '13 at 15:15
  • ugh, brainfart. you're right, i was thinking only the outer class declaration had to be static – Mike Corcoran Aug 06 '13 at 15:17
1

This is an extension method on HtmlHelper. As so, it should be called like this:

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

where TModel is the type assigned to the generic at the moment of declaration.

Please see also this question: MVC3 Razor DropDownListFor Enums

About Extension Methods, please see this: http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

Community
  • 1
  • 1
Dan
  • 1,060
  • 13
  • 39
1

This is an extension method on HtmlHelper. You can read more about it here.

You can call it lke this

 yourhtmlHelperObject.DropdownForEnum(someType,someName,label,attributes);
Ehsan
  • 31,833
  • 6
  • 56
  • 65