- Step 1
- Add enum to the project
- Define display text for each item in the enum.
-
public enum EnumFieldType { [Display(Name="Boolean")] BOOLEAN, [Display(Name = "Integer")] NUMERIC, [Display(Name = "Decimal")] DECIMAL, [Display(Name = "Free text")] TEXT, }
- Step 2
- Add in the view below dropdown list code
- @Html.DropDownListFor(model => model.FieldType,
from v in (ManageSurveys.Models.EnumFieldType[])(Enum.GetValues(typeof(ManageSurveys.Models.EnumFieldType)))
select new SelectListItem() {
Text = ((System.ComponentModel.DataAnnotations.DisplayAttribute)(typeof(ManageSurveys.Models.EnumFieldType).GetField(v.ToString()).GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false).FirstOrDefault())).Name,Value = v.ToString(), })
- This code will display appropriate text in the dropdown and select the int value of enum for save. Also while in edit it will select previously saved value.