DropDownList with enums

1 Answer 2415 Views
DropDownList
Steven
Top achievements
Rank 1
Steven asked on 29 Sep 2012, 09:27 PM
I am trying to use a DropDownList with an enum.

@Html.Kendo().DropDownListFor(model => model.Program) where Program is of type RecyclingProgram which is an Enum.

How do I get the enum values to:
  1. Display in the drop-down list?
  2. Update the model when it is posted back to the server?
Alexander
Top achievements
Rank 1
commented on 06 Dec 2012, 04:41 PM

This thread is kind of old but since it still came up in the search results:

If the name of your property is ePriority and PriorityEnum is an enum, then the following code will give you you all enum values in the drop down and bind correctly to your model:
Html.Kendo().DropDownList()
            .Name("ePriority")
            .BindTo(Enum.GetNames(typeof(PriorityEnum)).ToList()

This is equivalent to the following vanilla ASP.NET code (in function, not in the HTML it produces):
Html.DropDownListFor(m => m.ePriority, new SelectList(Enum.GetNames(typeof(PriorityEnum))))
The above is courtesy of: http://stackoverflow.com/questions/4656758/mvc3-razor-dropdownlistfor-enums/8538813#8538813
Chris
Top achievements
Rank 1
commented on 15 Jan 2018, 10:51 AM

Sadly this didn't work for me, it didn't prefill the drop down with the correct setting of the model (in the case of an edit). Anyone have a suggestion why?

1 Answer, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 18 Jan 2018, 08:50 AM
Hello Chris,

If I had correctly understood the issue that you are experiencing - the value is not correctly set when the DropDownListFor helper is used. If that's correct, I would suggest you to refer to the forum thread where a cause/solution for this case is discussed:

https://www.telerik.com/forums/problem-binding-enum-to-dropdownlist

Hope this would help.

Regards,
Nencho
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Steven
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Share this question
or