C#MVC:DropDownListFor绑定枚举一个整数模型场(C# MVC: DropDownL

2019-10-17 23:45发布

我如何绑定一个枚举类型整数模型场?

我尝试了一些扩展方法,但他们没有做好,导致所有方法所需的模型字段是给定的枚举类型...

下面是我的源(型号和枚举):

模型:

public class Einheit
{
    public Einheit()
    {
        Id = Guid.NewGuid();
    }

    public Guid Id { get; set; }
    public short TypeOfSomething { get; set; }
    public long Verwendungszweck { get; set; }
}

枚举:

public enum Einheitsart
{
    Type1 = 0,
    Type2 = 1,
    Type3 = 2,
    Type4 = 3,
    Type5 = 4,
    Type6 = 5
}

我想有值从0-6去(为了能保存整数在我的模型),但将DropDownList应显示文本“类型1”到“TYPE6” ...

我的问题是枚举转换到工作的SelectList。

谢谢!

Answer 1:

您可以枚举在所有枚举值,并为他们每个人创造SelectListItems。 这应该工作:

var selectList = new List<SelectListItem>();
foreach(Einheitsart art in Enum.GetValues(typeof(Einheitsart)))
{
    selectList.Add(new SelectListItem() { Value = (int)art, Text = art.ToString() })
}


Answer 2:

试试下面的帮手,我创建。

全部细节上可以看到我的博客:

http://www.ninjanye.co.uk/2012/01/creating-dropdown-list-from-enum-in.html

http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23

public static class EnumHelper  
{  
    //Creates a SelectList for a nullable enum value  
    public static SelectList SelectListFor<T>(T? selected)  
        where T : struct  
    {  
        return selected == null ? SelectListFor<T>()  
                                : SelectListFor(selected.Value);  
    }  

    //Creates a SelectList for an enum type  
    public static SelectList SelectListFor<T>() where T : struct  
    {  
        Type t = typeof (T);  
        if (t.IsEnum)  
        {  
            var values = Enum.GetValues(typeof(T)).Cast<enum>()  
                             .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });  

            return new SelectList(values, "Id", "Name");  
        }  
        return null;  
    }  

    //Creates a SelectList for an enum value  
    public static SelectList SelectListFor<T>(T selected) where T : struct   
    {  
        Type t = typeof(T);  
        if (t.IsEnum)  
        {  
            var values = Enum.GetValues(t).Cast<Enum>()  
                             .Select(e => new { Id = Convert.ToInt32(e), Name = e.GetDescription() });  

            return new SelectList(values, "Id", "Name", Convert.ToInt32(selected));  
        }  
        return null;  
    }   

    // Get the value of the description attribute if the   
    // enum has one, otherwise use the value.  
    public static string GetDescription<TEnum>(this TEnum value)  
    {  
        FieldInfo fi = value.GetType().GetField(value.ToString());  

        if (fi != null)  
        {  
            DescriptionAttribute[] attributes =  
             (DescriptionAttribute[])fi.GetCustomAttributes(  
    typeof(DescriptionAttribute),  
    false);  

            if (attributes.Length > 0)  
            {  
                 return attributes[0].Description;  
            }  
        }  

        return value.ToString();  
    }  
}  

要使用它只需使用下面的代码:

//If you don't have an enum value use the type  
ViewBag.DropDownList = EnumHelper.SelectListFor<MyEnum>();  

//If you do have an enum value use the value (the value will be marked as selected)  
ViewBag.DropDownList = EnumHelper.SelectListFor(myEnumValue); 

注:通过添加描述属性到你的枚举,它将使用作为下拉显示文本:

public enum Einheitsart
{
    Type1 = 0,
    [Description("2nd Type")]
    Type2 = 1,
    Type3 = 2,
    Type4 = 3,
    Type5 = 4,
    Type6 = 5
}


文章来源: C# MVC: DropDownListFor bind a enum to a integer model-field