Tuesday, 8 October 2013

bind enum values to dropdownlist in mvc3

First Create enum
public enum test
    {
        [Description("test1")]
        test1= 0,
        [Description("test2")]
        test= 1 ,
        [Description("test3")]
        test= 2 ,
        [Description("test4")]
        test= 3
    }

Assign Enum value to view

@Html.DropDownList("test", (from test in Enum.GetValues(typeof(test))
                                                      select new SelectListItem { Value = ((int)test).ToString(), Text = test.GetDescription() }))

1 comment:

  1. Simple example , its working fine. :)

    ReplyDelete