-2

I get an error when trying to pass a collection of Enums to a DropDownList.

The collection is of type IEnumerable.

The error states: "Cannot resolve method DropDownListFor( lambda expression, System.Collections.Generic.IEnumerable"

The code:

@Html.DropDownListFor(m => listing.WorkflowStatus, Model.WorkflowStatuses, new { id = listing.WorkflowStatus, onchange = "$(this.form).submit()" })

I'm completely stuck. Can anyone advice me on what the problem might be?

Subby
  • 5,370
  • 15
  • 70
  • 125
  • 1
    possible duplicate of [MVC3 Razor DropDownListFor Enums](http://stackoverflow.com/questions/4656758/mvc3-razor-dropdownlistfor-enums) – James Jan 18 '13 at 10:52
  • No it's not a duplicate. Thank you for pointing me to that thread. – Subby Jan 18 '13 at 11:00
  • I know it's not an *exact* duplicate, but it's close enough :) – James Jan 18 '13 at 11:01
  • nice attitude, you are asking for help - the link provided is *exactly* what you are looking for, I don't really see what the problem is. – James Jan 18 '13 at 11:07
  • I have absolutely no intention of helping you now anyway, just a word of advice for the future - don't take it personally when someone *suggests* that your question is a duplicate (especially when it practically is). Or better yet, put some effort in and search for similar issues *before* posting then that way it can't be flagged. – James Jan 18 '13 at 11:54
  • Go... Troll.. Somewhere else. – Subby Jan 18 '13 at 12:27
  • think we both know who the troll is here... – James Jan 18 '13 at 12:36
  • 2
    James don't bother, there is no point wasting your time. – free4ride Jan 18 '13 at 15:02

1 Answers1

1

Check out a helper I made to do just this.

http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23

You need to turn them into a select list

In your controller, convert your enum to an IEnumerable and add it to your ViewBag then reference it in your view

Controller:

ViewBag.WorkflowStatuses = EnumHelper.SelectListFor(WorkflowStatus.Option1);

In the view (something like....)

@Html.DropDownListFor(m => listing.WorkflowStatus, ViewBag.WorkflowStatuses as IEnumerable<SelectListItem>.....
NinjaNye
  • 7,046
  • 1
  • 32
  • 46
  • You may not need the entire helper but you should be able to get from it what you need. If you want more readable names in your drop down, add the `DescriptionAttribute` to your enums – NinjaNye Jan 18 '13 at 11:09
  • Hi Nye, the code: ViewBag.WorkflowStatuses = EnumHelper.SelectListFor(WorkflowStatus.Option1); only adds one enum type.. How do I add all of the types in the enum? – Subby Jan 18 '13 at 12:29
  • It should add all based on the type... `ViewBag.WorkflowStatuses = EnumHelper.SelectListFor()`; – NinjaNye Jan 18 '13 at 13:56