Click here to Skip to main content
14,699,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a list of enums like mention below.
public enum AccountCategory
   {
       [EnumMember]
       System_Owner = 2,
       [EnumMember]
       System_Account = 3,
       [EnumMember]
       Customer = 4,
       [EnumMember]
       Supplier = 5
   }
public enum ProductStatus
    {
        
        [EnumMember]
        _Any = -1,
        [EnumMember]
        Unknown = 0,
        [EnumMember]
        Available = 1,
        [EnumMember]
        Unde_Offer = 2,
         [EnumMember]
        Reserved = 3,
        [EnumMember]
        Unavailable = 4,
        [EnumMember]
        Price_Reduction = 5,
         [EnumMember]
        Guide_Price = 6
    }


when page load only show the Enum Name in the dropdown when user select the enum it value show in another dropdown list

What I have tried:

public ActionResult Index
        {
            List<SelectList> list = new List<SelectList>();

            var references = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsEnum && t.IsPublic).OrderBy(t => t.Name);

            List<SelectListItem> items = references.Select(r =>
                new SelectListItem()
                {
                    Value = r.Name,
                    Text = r.Name
                }).ToList();
            ViewBag.Enums = items;
            return View();
        }

   public JsonResult LoadEnumsValue(string EnumName)
        {
            List<SelectListItem> item = new List<SelectListItem>();
            try
            {
                if (Enum != null && Enum != "")
                {
                     var references = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsEnum && t.IsPublic).OrderBy(t => t.Name);
                    if (references.Any())
                    {
                        var EnumValue = references.Where(x => x.Name.ToLower().Equals(Enum.ToLower()));
                        if (EnumValue.Any())
                        {
                            item = EnumValue.Select(r =>
                            new SelectListItem()
                            {
                                Value = r.Name,
                                Text = r.Name
                            }).ToList();
                        }
                    }

                }
            }
            catch (Exception ex)
            {


            }
            return Json(item);
        }
Posted
Updated 1-Jun-16 14:13pm
v2

1 solution

Please see my articles on the topic. Basically, you need only first two articles from my enumeration series. If you are interest, you can read the rest; the are referenced on all articles from this series:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations,
Human-readable Enumeration Meta-data.

The idea is: you can fetch all you need from the assembly metadata; and it's enough to do it just once, so the performance won't suffer.

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900