Asp.net mvc 4 用于问题的枚举DropDownList

Asp.net mvc 4 用于问题的枚举DropDownList,asp.net-mvc-4,enums,html.dropdownlistfor,Asp.net Mvc 4,Enums,Html.dropdownlistfor,我读过一个可能的解决方案,但需要大量重写,可能的解决方案是链接的,但如果我在DropDownlist for中只写了几个字,那么这样做就没有任何意义了 我的dropdownlistfor有一个问题,因为这对我来说是全新的: @Html.DropDownListFor(model => model.pageID, new SelectList (Enum.GetNames(typeof(PageIndex)), EnumHelper.GetSelectedItemList<PageIn

我读过一个可能的解决方案,但需要大量重写,可能的解决方案是链接的,但如果我在DropDownlist for中只写了几个字,那么这样做就没有任何意义了

我的dropdownlistfor有一个问题,因为这对我来说是全新的:

@Html.DropDownListFor(model => model.pageID, new SelectList (Enum.GetNames(typeof(PageIndex)), EnumHelper.GetSelectedItemList<PageIndex>().SelectedValue))
我的助手:

public class EnumHelper
{
    public static SelectList GetSelectedItemList<T>() where T : struct
    {
        T t = default(T);
        if (!t.GetType().IsEnum) { throw new ArgumentNullException("Please make sure that T is of Enum Type"); }

        var nameList = t.GetType().GetEnumNames();

        int counter = 0;

        Dictionary<int, String> myDictionary = new Dictionary<int, string>();
        if (nameList != null && nameList.Length > 0)
        {
            foreach (var name in nameList)
            {
                T newEnum = (T) Enum.Parse(t.GetType(), name);
                string description = getDescriptionFromEnumValue(newEnum as Enum);

                if (!myDictionary.ContainsKey(counter))
                {
                    myDictionary.Add(counter, description);
                }
                counter++;
            }

            counter = 0;

            return new SelectList(myDictionary, "Key", "Value");
        }

        return null;
    }

    private static string getDescriptionFromEnumValue(Enum value)
    {
        DescriptionAttribute descriptionAttribute =
            value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false)
            .SingleOrDefault() as DescriptionAttribute;

        return descriptionAttribute == null ? 
            value.ToString() : descriptionAttribute.Description;
    }
}
公共类枚举帮助器
{
public static SelectList GetSelectedItemList(),其中T:struct
{
T=默认值(T);
如果(!t.GetType().IsEnum){抛出新的ArgumentNullException(“请确保t是枚举类型”);}
var nameList=t.GetType().GetEnumNames();
int计数器=0;
Dictionary myDictionary=新字典();
if(nameList!=null&&nameList.Length>0)
{
foreach(名称列表中的变量名称)
{
T newEnum=(T)Enum.Parse(T.GetType(),name);
字符串描述=getDescriptionFromEnumValue(newEnum作为Enum);
如果(!myDictionary.ContainsKey(计数器))
{
添加(计数器、说明);
}
计数器++;
}
计数器=0;
返回新的选择列表(myDictionary,“Key”,“Value”);
}
返回null;
}
私有静态字符串getDescriptionFromEnumValue(枚举值)
{
description属性description属性=
value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute),false)
.SingleOrDefault()作为DescriptionAttribute;
返回descriptionAttribute==null?
value.ToString():descriptionAttribute.Description;
}
}

您能使用ASP.NET 5.1吗?我采用了不同的方法,不过感谢您回顾我的问题。
public class EnumHelper
{
    public static SelectList GetSelectedItemList<T>() where T : struct
    {
        T t = default(T);
        if (!t.GetType().IsEnum) { throw new ArgumentNullException("Please make sure that T is of Enum Type"); }

        var nameList = t.GetType().GetEnumNames();

        int counter = 0;

        Dictionary<int, String> myDictionary = new Dictionary<int, string>();
        if (nameList != null && nameList.Length > 0)
        {
            foreach (var name in nameList)
            {
                T newEnum = (T) Enum.Parse(t.GetType(), name);
                string description = getDescriptionFromEnumValue(newEnum as Enum);

                if (!myDictionary.ContainsKey(counter))
                {
                    myDictionary.Add(counter, description);
                }
                counter++;
            }

            counter = 0;

            return new SelectList(myDictionary, "Key", "Value");
        }

        return null;
    }

    private static string getDescriptionFromEnumValue(Enum value)
    {
        DescriptionAttribute descriptionAttribute =
            value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false)
            .SingleOrDefault() as DescriptionAttribute;

        return descriptionAttribute == null ? 
            value.ToString() : descriptionAttribute.Description;
    }
}