locked
EnumDropDownListFor With Group Type RRS feed

  • Question

  • User1865809606 posted

    public enum ImportType
    {
    [Display(Name = "Daily Transaction", GroupName = "Finance")]
    DailyTransaction = 1,
    [Display(Name = "Provider Queue", GroupName = "Technical")]
    ProviderQueue = 2,
    [Display(Name = "Preferred Provider", GroupName = "Manage Property")]
    PreferredProvider = 3,

     [Display(Name = "Cache Feed", GroupName = "Technical")]
    CacheFeed = 4,
    [Display(Name = "CJ Property Feed", GroupName = "Affiliate Submit")]
    CjFeed = 5

    }

    I have above type of Enum and I use below code in my razor page

    @Html.EnumDropDownListFor(
    model => model.ImportTypes,
    "Select Import Type",
    new { @class = "form-control col-md-7 col-xs-12", required = "required" })

    but I am not able to see group in this dropdown.

    Please see link as a result of this code https://snag.gy/XzAq5I.jpg.

    but I need the result as https://snag.gy/d1OYq7.jpg , https://github.com/aspnet/Mvc/issues/2292

    Let me know where the issue. I tried many ways but not able to get a group in my dropdown list.

    Friday, August 9, 2019 7:56 PM

All replies

  • User665608656 posted

    Hi cpate,

    According to your code and description, what I need to confirm with you is which version of MVC are you using?

    Different versions of MVC have different ways of writing.  Your current usage needs to be above MVC 5.1.

    If you can, it is recommended that you upgrade your MVC version to 5.1 to ensure the normal operation of the code.

    For versions below MVC 5.1, you can refer to the following links for modifications:

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

    Best Regards,

    YongQing.

    Monday, August 12, 2019 8:33 AM
  • User1865809606 posted

    I have MVC 5.1

    I need Group in EnumDropDownListFor.

    Monday, August 12, 2019 1:15 PM
  • User665608656 posted

    Hi cpatel,

    After many tests, I found that you need to use GetEnumSelectList in EnumDropDownListFor to group it, but mvc5.1 does not support GetEnumSelectList.

    So I recommend that you use DropDownListFor and rewrite the EnumHelper class.

    First create EnumHelper.cs in your MVC project:

     public static class EnumHelper<T>
        {
            static EnumHelper()
            {
                var enumType = typeof(T);
                if (!enumType.IsEnum) { throw new ArgumentException("Type '" + enumType.Name + "' is not an enum"); }
            }
    
            public static string GetEnumDescription(T value)
            {
                var fi = typeof(T).GetField(value.ToString());
                var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return attributes.Length > 0 ? attributes[0].Description : value.ToString();
            }
    
            public static IEnumerable<SelectListItem> GetSelectList()
            {
                var groupDictionary = new Dictionary<string, SelectListGroup>();
    
                var enumType = typeof(T);
                var fields = from field in enumType.GetFields()
                             where field.IsLiteral
                             select field;
    
                foreach (var field in fields)
                {
                    var display = field.GetCustomAttribute<DisplayAttribute>(false);
                    var description = field.GetCustomAttribute<DescriptionAttribute>(false);
                    var group = field.GetCustomAttribute<CategoryAttribute>(false);
    
                    var text = display?.GetName() ?? display?.GetShortName() ?? display?.GetDescription() ?? display?.GetPrompt() ?? description?.Description ?? field.Name;
                    var value = field.Name;
                    var groupName = display?.GetGroupName() ?? group?.Category ?? string.Empty;
                    if (!groupDictionary.ContainsKey(groupName)) { groupDictionary.Add(groupName, new SelectListGroup { Name = groupName }); }
    
                    yield return new SelectListItem
                    {
                        Text = text,
                        Value = value,
                        Group = groupDictionary[groupName],
                    };
                }
            }
        }

    Then create a model named  Employee to put enum data:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication_mvc5._1.Models
    {
        public enum EmploymentType
        {
            [Display(Name = "Daily Transaction", GroupName = "Finance")]
            DailyTransaction = 1,
            [Display(Name = "Provider Queue", GroupName = "Finance")]
            ProviderQueue = 2,
            [Display(Name = "Preferred Provider", GroupName = "Manage Property")]
            PreferredProvider = 3,
    
            [Display(Name = "Cache Feed", GroupName = "Technical")]
            CacheFeed = 4,
            [Display(Name = "CJ Property Feed", GroupName = "Affiliate Submit")]
            CjFeed = 5
        }
    
        public class Employee
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public EmploymentType EmploymentType { get; set; }
        }
    }

    Here is the view:

    @model WebApplication_mvc5._1.Models.Employee
    @{
        ViewBag.Title = "Index";
    }
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <h2>Index</h2>
    
    <div>
        @Html.DropDownListFor(model => model.EmploymentType, EnumHelper<WebApplication_mvc5._1.Models.EmploymentType>.GetSelectList(), new { @class = "form-control col-md-7 col-xs-12", required = "required" })
    
    </div>
    <br />

    You can refer to this link : how to use enum with DescriptionAttribute in asp.net mvc

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    Wednesday, August 14, 2019 10:00 AM
  • User1865809606 posted

    Thanks for your efforts,

    I know this extension class, but I want to out of box functionality for enum dropdown without converting it into select list

    Wednesday, August 14, 2019 1:12 PM
  • User665608656 posted

    Hi cpatel,

    I know this extension class, but I want to out of box functionality for enum dropdown without converting it into select list

    I'm sorry to tell you that EnumDropDownListFor can not automatically bind group.

    The last reply is the better way I've found to implement dropdownlist grouping.

    Best Regards,

    YongQing.

    Friday, August 16, 2019 5:15 AM