将枚举值传递到ASP.NET MVC3中的ListBoxFor [英] Passing enum values to a ListBoxFor in ASP.NET MVC3

查看:83
本文介绍了将枚举值传递到ASP.NET MVC3中的ListBoxFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于从Enum创建SelectList以填充下拉列表的内容,我已经学到了很多知识,并找到了许多解决方案.这里有几个例子

I've read up a good bit on creating a SelectList from an Enum to populate a drop down list, and found many solutions. Here are a few examples

public static IEnumerable<SelectListItem> GetEnumSelectList<T>()
{
    return (Enum.GetValues(typeof(T)).Cast<T>().Select(
        enu => new SelectListItem() { Text = enu.ToString(), Value = enu.ToString() })).ToList();
}

public static SelectList ToSelectList<TEnum>(this TEnum enumObj) 
    where TEnum : struct, IComparable, IFormattable, IConvertible
{
    var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                 select new { Id = e, Name = e.ToString() };
    return new SelectList(values, "Id", "Name", enumObj);
}

这两个(以及大多数其他)都返回TextValue的枚举值的名称.

Both of these (and most others) return the names of the enum values for both Text and Value.

示例

public enum WeatherInIreland
{
     NotGreat = 0,
     Bad = 1,
     Awful = 2
} 

前两种方法的结果

<select id="Weather" name="Weather">
    <option value="NotGreat">NotGreat</option>
    <option value="Bad">Bad</option>
    <option value="Awful">Awful</option>
</select>

但是,我想为Text返回名称,为Value返回 int值.

However, I wanted to return the name for Text and the int value for the Value.

<select id="Weather" name="Weather">
    <option value="0">NotGreat</option>
    <option value="1">Bad</option>
    <option value="2">Awful</option>
</select>

这是我想出的代码.

public static System.Web.Mvc.SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible
{
    List<SelectListItem> selist = new List<SelectListItem>();
    foreach (int value in Enum.GetValues(typeof(TEnum)))
    {
        TEnum type = (TEnum)Enum.Parse(typeof(TEnum), value.ToString());
        selist.Add(new SelectListItem { Value = value.ToString(), Text = type.ToString() });
    }
    return new System.Web.Mvc.SelectList(selist, "Value", "Text");
}

如您所见,它是上面最后一个方法的修改版本.它可以工作,但是很难看.我必须将Enum值迭代为整数,然后分别解析每个值以返回名称.当然有更好的方法吗?

As you can see it's a modified version of the last method above. It works, but it is ugly. I have to iterate over the Enum values as ints and then separately parse each one to return the name. Surely there's a better way of doing this?

推荐答案

不幸的是,实际上并没有更好的方法.在理想情况下,Enum将实现IEnumerable,您可以遍历集合.相反,您只能通过Enum.GetValuesEnum.GetNames进行迭代.无论您选择哪种方式,都仍然需要调用Enum.GetNameEnum.GetValue来获取其他数据.

Unfortunately there's not really a better way to do this. In a perfect world, Enum would implement IEnumerable and you could iterate through the collection. Instead you can only either iterate through Enum.GetValues or Enum.GetNames. Whichever you choose, you'll still need to call Enum.GetName or Enum.GetValue to get the other piece of data.

类似的StackOverflow问题: C#遍历枚举? (索引System.Array)

Similiar StackOverflow question: C# Iterating through an enum? (Indexing a System.Array)

不过,您可以使代码更具可读性并且linq-y:

You can however make the code a bit more readable and linq-y:

 return
      new SelectList(
          Enum.GetValues(typeof (TEnum))
              .Select(e => new SelectListItem
              {
                 Text = Enum.GetName(typeof (TEnum), e),
                 Value = e.ToString()
              })
              .ToList(),
          "Value",
          "Text");

这篇关于将枚举值传递到ASP.NET MVC3中的ListBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆