1

I have a dropdown control inside my view. Now i want it to have Description of enum as it's text. I am least worried about Id but i need text to be well formatted.

skp
  • 25
  • 5
  • Do you mean a description as defined by a `DescriptionAttribute`. Show the enum definition and how you generate the dropdown in the view –  Feb 25 '15 at 09:35
  • possible duplicate of http://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc – Jenish Rabadiya Feb 25 '15 at 09:39

2 Answers2

3

Refer the following code.

public static List<EnumModel> GetEnumList<T>()
            {
                var enumValues = Enum.GetValues(typeof(T)).Cast<T>().Select(rentalType => new EnumModel()
                {
                    Value = Convert.ToInt32(rentalType),
                    Name = GetDisplayName<T>(rentalType, false)
                }).ToList();

                return enumValues;
            }

            public static string GetDisplayName<T>(T value, bool isDisplayName)
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value", "Enum value Empty");
                }

                var type = value.GetType();
                var field = type.GetField(value.ToString());
                if (field == null)
                {
                    return value.ToString();
                }

                var attributes = ((DisplayAttribute[])field.GetCustomAttributes(typeof(DisplayAttribute), false)).FirstOrDefault();
                return attributes != null ? isDisplayName == true ? attributes.GetDescription() : attributes.Description : value.ToString();
            }

     public class EnumModel
            {
                /// <summary>
                /// Gets or sets the value
                /// </summary>
                public int Value { get; set; }

                /// <summary>
                /// Gets or sets the name
                /// </summary>
                public string Name { get; set; }
            }

You can get List<EnumModel> as ENUM list with name and value.What you need to do is just make a List<SelectListitem> from List<EnumModel>

Hope this helps.

Frebin Francis
  • 1,905
  • 1
  • 11
  • 19
  • Yes thank you all for quick response. This is precisely what i was looking for.Sorry but i can't upvote you guys as i don't have enough reputation. – skp Feb 25 '15 at 09:54
2

Use this code and bind with your dropdown-

public static List<SelectListItem> GetSelectList(Type enumType, String SelectedValue, Boolean IsValString = true)
            {
            Array values = Enum.GetValues(enumType);
            List<SelectListItem> selectListItems = new List<SelectListItem>(values.Length);

            foreach (var i in Enum.GetValues(enumType))
            {
                String name = Enum.GetName(enumType, i);
                String desc = name;
                FieldInfo fi = enumType.GetField(name);
                var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                String result = attributes.Length == 0 ? desc : ((DescriptionAttribute)attributes[0]).Description;
                var selectItem = new SelectListItem()
                {
                    Text = result,
                    Value = (IsValString) ? i.ToString() : ((Int32)i).ToString()
                };

                if ((SelectedValue != null) && (SelectedValue.Equals(selectItem.Value)))
                {
                    selectItem.Selected = true;
                }

                selectListItems.Add(selectItem);
            }
            return selectListItems;
        }
Utkarsh
  • 384
  • 3
  • 6