Search code examples
asp.net-mvcdropdownlistfor

DropDownList with Enums


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?


Solution

  • 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>.....