How do you bind an Enum to a DropDownList control in ASP.NET?

后端 未结 25 2336
既然无缘
既然无缘 2020-11-29 15:41

Let\'s say I have the following simple enum:

enum Response
{
    Yes = 1,
    No = 2,
    Maybe = 3
}

How can I bind this enum to a DropDow

相关标签:
25条回答
  • 2020-11-29 16:10

    I use this for ASP.NET MVC:

    Html.DropDownListFor(o => o.EnumProperty, Enum.GetValues(typeof(enumtype)).Cast<enumtype>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }))
    
    0 讨论(0)
  • 2020-11-29 16:11

    You could use linq:

    var responseTypes= Enum.GetNames(typeof(Response)).Select(x => new { text = x, value = (int)Enum.Parse(typeof(Response), x) });
        DropDownList.DataSource = responseTypes;
        DropDownList.DataTextField = "text";
        DropDownList.DataValueField = "value";
        DropDownList.DataBind();
    
    0 讨论(0)
  • 2020-11-29 16:11

    This is probably an old question.. but this is how I did mine.

    Model:

    public class YourEntity
    {
       public int ID { get; set; }
       public string Name{ get; set; }
       public string Description { get; set; }
       public OptionType Types { get; set; }
    }
    
    public enum OptionType
    {
        Unknown,
        Option1, 
        Option2,
        Option3
    }
    

    Then in the View: here's how to use populate the dropdown.

    @Html.EnumDropDownListFor(model => model.Types, htmlAttributes: new { @class = "form-control" })
    

    This should populate everything in your enum list. Hope this helps..

    0 讨论(0)
  • 2020-11-29 16:13

    I am not sure how to do it in ASP.NET but check out this post... it might help?

    Enum.GetValues(typeof(Response));
    
    0 讨论(0)
  • 2020-11-29 16:14
    public enum Color
    {
        RED,
        GREEN,
        BLUE
    }
    

    Every Enum type derives from System.Enum. There are two static methods that help bind data to a drop-down list control (and retrieve the value). These are Enum.GetNames and Enum.Parse. Using GetNames, you are able to bind to your drop-down list control as follows:

    protected System.Web.UI.WebControls.DropDownList ddColor;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
         if(!IsPostBack)
         {
            ddColor.DataSource = Enum.GetNames(typeof(Color));
            ddColor.DataBind();
         }
    }
    

    Now if you want the Enum value Back on Selection ....

      private void ddColor_SelectedIndexChanged(object sender, System.EventArgs e)
      {
        Color selectedColor = (Color)Enum.Parse(typeof(Color),ddColor.SelectedValue
      }
    
    0 讨论(0)
  • 2020-11-29 16:17

    If you would like to have a more user friendly description in your combo box (or other control) you can use the Description attribute with the following function:

        public static object GetEnumDescriptions(Type enumType)
        {
            var list = new List<KeyValuePair<Enum, string>>();
            foreach (Enum value in Enum.GetValues(enumType))
            {
                string description = value.ToString();
                FieldInfo fieldInfo = value.GetType().GetField(description);
                var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).First();
                if (attribute != null)
                {
                    description = (attribute as DescriptionAttribute).Description;
                }
                list.Add(new KeyValuePair<Enum, string>(value, description));
            }
            return list;
        }
    

    Here is an example of an enum with Description attributes applied:

        enum SampleEnum
        {
            NormalNoSpaces,
            [Description("Description With Spaces")]
            DescriptionWithSpaces,
            [Description("50%")]
            Percent_50,
        }
    

    Then Bind to control like so...

            m_Combo_Sample.DataSource = GetEnumDescriptions(typeof(SampleEnum));
            m_Combo_Sample.DisplayMember = "Value";
            m_Combo_Sample.ValueMember = "Key";
    

    This way you can put whatever text you want in the drop down without it having to look like a variable name

    0 讨论(0)
提交回复
热议问题