1

列挙型を整数型のモデルフィールドにバインドするにはどうすればよいですか?

いくつかの拡張メソッドを試しましたが、うまくいきませんでした。すべてのメソッドで、モデル フィールドが特定の列挙型である必要がありました...

これが私のソースです(モデルと列挙型):

モデル:

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 には "Type1" から "Type6" までのテキストが表示されるはずです...

私が抱えている問題は、列挙型を機能する SelectList に変換することです。

ありがとうございました!

4

2 に答える 2

1

私が作成した次のヘルパーを試してください。

完全な詳細は私のブログで見ることができます:

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
}
于 2013-04-09T10:49:31.330 に答える
0

すべての列挙値を列挙し、それぞれの SelectListItems を作成できます。これはうまくいくはずです:

var selectList = new List<SelectListItem>();
foreach(Einheitsart art in Enum.GetValues(typeof(Einheitsart)))
{
    selectList.Add(new SelectListItem() { Value = (int)art, Text = art.ToString() })
}
于 2013-04-09T09:47:31.297 に答える