Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind Enum Types to the DropDownList?

Tags:

c#

enums

asp.net

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?

like image 859
Hotmoil Avatar asked Jun 23 '10 02:06

Hotmoil


People also ask

How do you create a DropDownList from an enum in ASP NET MVC?

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.

How do I turn an enum into a list?

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.


1 Answers

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); 
like image 72
Amr Badawy Avatar answered Sep 25 '22 19:09

Amr Badawy