MVC 3 Razor - How to fill Html.X().MultiCombo with an enum

  1. #1

    MVC 3 Razor - How to fill Html.X().MultiCombo with an enum

    Hi all,

    What is the best way to fill a Html.X().MultiCombo with an enum.

    how do i define the store reader for an enum?

    My enum looks likem this
    public enum ServiceStatus
            {
                Undefined   = 1,            
                InDelivery  = 2,            
                Testing     = 3,            
                Operational = 4,            
                Degraded    = 5,
                Cancelling  = 6,
                Archived    = 7,
                InOrder     = 8
            }
    
    
    
    UI Code 
                                      Html.X().MultiCombo()
                                        .FieldLabel("Status")
                                        .LabelAlign(LabelAlign.Top)
                                        .ID("mcbBillingStatus")
                                        .Margins("0 0 0 0")
                                        .LabelWidth(200)
                                        .Width(400)
                                        .EmptyText("--- Please Select ---")
                                        .EmptyValue("-1")
                                        .Cls("form-label")
                                                                           
                                       .Items(items =>
                                        {
                                            items.Add(new ListItem()
                                            {
                                                Text =ServiceStatus.Billing.ToString(),
                                                Value = ((int)ServiceStatus.Billing).ToString()
                                            });
    
                                            items.Add(new ListItem()
                                            {
                                                Text = ServiceStatus.CeasedBilling.ToString(),
                                                Value = ((int)ServiceStatus.CeasedBilling).ToString()
                                            });
    
    
                                        }));
    Im not happy about the above code and how do i define the store reader for an enum



    Regards
    Ish
    Last edited by Daniil; Oct 25, 2013 at 12:59 PM. Reason: Please use [CODE] tags
  2. #2
    You can use the following helper method
    public static ListItemCollection FromEnum(Type enumType)
            {
                ListItemCollection items = new ListItemCollection();
                foreach (var item in Enum.GetValues(enumType))
                {
                    var fi = enumType.GetField(item.ToString());
                    var attribute = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
                    var title = attribute == null ? item.ToString() : ((DescriptionAttribute)attribute).Description;
                    var value = Convert.ChangeType(item, ((Enum)item).GetTypeCode());
                    var listItem = new ListItem
                    {
                        Value = value.ToString(),
                        Text = title
                    };
                    items.Add(listItem);
                }
    
    
                return items;
            }
    In Ext.Net 2.1 you can use the following code
    Html.X().ComboBox().ItemsFromEnum(typeof(MyEnum))
  3. #3
    Thank you Vladimir

    Thank you for pointing me to the right direction! ItemsFromEnum method can be used directly by basing the enum

    Ext.Net 2.1 you can use the following code
    Html.X().ComboBox().ItemsFromEnum(typeof(Models.Se rviceCommercialStatus)));

    Regards
    Ish


    Quote Originally Posted by Vladimir View Post
    You can use the following helper method
    public static ListItemCollection FromEnum(Type enumType)
            {
                ListItemCollection items = new ListItemCollection();
                foreach (var item in Enum.GetValues(enumType))
                {
                    var fi = enumType.GetField(item.ToString());
                    var attribute = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
                    var title = attribute == null ? item.ToString() : ((DescriptionAttribute)attribute).Description;
                    var value = Convert.ChangeType(item, ((Enum)item).GetTypeCode());
                    var listItem = new ListItem
                    {
                        Value = value.ToString(),
                        Text = title
                    };
                    items.Add(listItem);
                }
    
    
                return items;
            }
    In Ext.Net 2.1 you can use the following code
    Html.X().ComboBox().ItemsFromEnum(typeof(MyEnum))

Similar Threads

  1. Replies: 1
    Last Post: May 21, 2012, 8:36 AM
  2. Replies: 3
    Last Post: Feb 21, 2012, 7:46 AM
  3. [CLOSED] Fill a store with an enum
    By 78fede78 in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 10, 2010, 4:50 PM
  4. [CLOSED] Fill a combobox with an enum.
    By capecod in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Oct 22, 2010, 2:06 PM
  5. combobox fill dynamically uing enum
    By Prasad in forum 1.x Help
    Replies: 0
    Last Post: Aug 19, 2010, 12:29 PM

Tags for this Thread

Posting Permissions