If I have the following enum
public enum EmployeeType { Manager = 1, TeamLeader, Senior, Junior }
and I have DropDownList and I want to bind this EmployeeType
enum to the DropDownList, is there any way to do that?
Run the application and we get the results as in Figure 1.1. I create an HTML Helper extension method to populate a dropdown list using an enum. Create an extension method EnumDropDownListFor in the Extension class under the Models folder. Create a model (ActionTypeModel class) under the Models folder.
The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.
if you have DropDownList object called ddl you can do it as below
ddl.DataSource = Enum.GetNames(typeof(EmployeeType)); ddl.DataBind();
if you want the Enum value Back on Selection ....
EmployeeType empType = (EmployeeType)Enum.Parse(typeof(EmployeeType), ddl.SelectedValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With