c#枚举名称在DropDownList而不是值进行过滤 [英] c# enum names to filter on in a DropDownList instead of value

查看:200
本文介绍了c#枚举名称在DropDownList而不是值进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举列表,其中包含一个名为Default的枚举。

  public enum Fruits {Default = Banana,
Banana = 1,
Orange = 2,
葡萄柚= 3}

我需要填充排除默认字段的下拉列表。 >

  Html.DropDownList(fruitSelector,
Enum.GetValues(typeof(Fruits))。Cast< Fruits>()
.OrderBy(o => o.GetDescription())
.Where(o => o!= Fruits.Default)
.Select(o => new SelectListItem() Text = o.GetDescription(),Value =((int)o).ToString()}), - 所有水果 - ,
new {@class =form-control,@ aria_describedby = sizing-addon1})

当我尝试过滤Fruits.Default它删除BOTH默认和香蕉。我如何进行一个过滤器比较,我只删除默认值?



[[CORRECTION]]这个枚举重复了我的问题。我真的不明白差异。

  public enum Fruits 
{
默认= Peaches,
Peaches = 1,
香蕉= 2,
葡萄柚= 3,
苹果= 101,
Watermellon = 102
}

  public enum Fruits2 
{
默认= Mangos,
Mangos = 1,
日期= 2,
图= 3,
苹果= 101,
Limes = 102,
葡萄= 103
}


解决方案

我不认为你想做的是可能的,因为它是。问题是你的枚举结构。

  public enum Fruits 
{
默认= Peaches,
Peaches = 1,
香蕉= 2,
葡萄柚= 3,
Apple = 101,
Watermellon = 102
}

你说的价值默认值 Peaches 其值依次为 1 。其中基本上是相同的( B Peaches A 默认

  int B = 1; 
int A = B;

所以当我们使用 Enum.Parse() / code>,它同时处理,并选择第一个值,当您查看以下情况时,您将了解如何运行以下代码:使用当前的枚举:

  var deflt = Enum.Parse(typeof(Fruits),Fruits.Default.ToString()); 
var peach = Enum.Parse(typeof ),Fruits.Peaches.ToString());
Console.WriteLine(deflt);
Console.WriteLine(peach);

输出将是:


默认值



默认


然后将您的枚举更改为:

  public enum Fruits 
{
Peaches = Default,
默认值= 1,
香蕉= 2,
葡萄柚= 3,
Apple = 101,
Watermellon = 102
}

并运行上述两个行代码再次。您的输出将是:


桃子



桃子


也就是说, Parse()选择第一个定义的枚举, 。



编辑:



上述说明略有不正确,在乔治亚历山大下面的评论。请参阅他的解释:
使用Peaches = Default,Default = 1,Default = Peaches,Peaches = 1时,您的输出不同,因为您在Console.WriteLine中调用Enum.ToString。如果你看到var deflt,var peach,你会看到它们在两种情况下是一样的,它们的名字是第一个按字母顺序排列的值。






但是有一个解决方法。从你的初始代码示例中我收集的是你需要列举的枚举,它们的描述和数值,我是正确的吗?所以,我首先要抓住字符串的列表,不包括 Default ,这很简单。然后遍历字符串列表,并添加到您的 SelectListItem 类对象的列表。

  var res = Enum.GetNames(typeof(Fruits))
.Where(x => x!=Default);

列表< SelectListItem> list = new List< SelectListItem>();
foreach(string fruit in res)
{
string desc = EnumHelper< Fruits> .GetEnumDescription(fruit); //这是一个我使用的实用程序。您可以使用扩展方法获取描述。
int val =(int)Enum.Parse(typeof(Fruits),fruit);
list.Add(new SelectListItem {enumName = fruit,enumDesc = desc,enumVal = val});
}

如果你必须有 OrderBy ,创建 SelectListItem 列表后,再次按描述


I have a list of enums that includes a enumeration called Default.

public enum Fruits { Default = Banana, 
                    Banana = 1, 
                    Orange = 2, 
                    Grapefruit = 3 }

I need to populate a dropdownlist that excludes the Default field.

Html.DropDownList("fruitSelector", 
                Enum.GetValues(typeof(Fruits)).Cast<Fruits>()
                    .OrderBy(o => o.GetDescription())
                    .Where(o => o != Fruits.Default)
                    .Select(o => new SelectListItem() {Text = o.GetDescription(), Value = ((int) o).ToString()}), "-- All Fruits --",
                new {@class = "form-control", @aria_describedby="sizing-addon1"})

when i try to filter on Fruits.Default it removes BOTH default and the Banana. How can i do a filter comparison where i only remove Default?

[[CORRECTION]] this enum duplicates my problem. I honestly don't understand the difference.

public enum Fruits
    {
        Default = Peaches,
        Peaches = 1,
        Bananas = 2,
        Grapefruit = 3,
        Apple = 101,
        Watermellon = 102
    }

or

public enum Fruits2
{
    Default = Mangos,
    Mangos = 1,
    Dates = 2,
    Figs = 3,
    Apples = 101,
    Limes = 102,
    Grapes = 103
}

解决方案

I don't think what you're trying to do is possible as it is. The problem is your Enum structure.

public enum Fruits
{
    Default = Peaches,
    Peaches = 1,
    Bananas = 2,
    Grapefruit = 3,
    Apple = 101,
    Watermellon = 102
}

You're saying value of Default is Peaches of which value in turn is 1. Which essentially is the same as (B is Peaches and A is Default:

int B = 1;
int A = B;

So when we use Enum.Parse(), it treats both the same, and picks the first value. You'll understand when you look at the following scenario. Run the following code, with your current Enum:

var deflt = Enum.Parse(typeof(Fruits), Fruits.Default.ToString());
var peach = Enum.Parse(typeof(Fruits), Fruits.Peaches.ToString());
Console.WriteLine(deflt);
Console.WriteLine(peach);

The output is going to be:

Default

Default

Then change your Enum to this:

public enum Fruits
{
    Peaches = Default ,
    Default = 1,
    Bananas = 2,
    Grapefruit = 3,
    Apple = 101,
    Watermellon = 102
}

And run the above two lines of code again. Your output this time will be:

Peaches

Peaches

That is, Parse() picks the first defined Enum when there's an equality like you have.

EDIT:

The above explanation is slightly incorrect, as pointed out in the comments below by George Alexandria. Please see his explanation: You have the different output when use Peaches = Default , Default = 1, and Default = Peaches, Peaches = 1, because you invoke Enum.ToString in Console.WriteLine. If you look at the var deflt, var peach you will see that they are the same in the both cases and they are the value which name is the first by alphabetical.


But there is a workaround. From your initial code sample what I gather is you need a list of Enums, their descriptions, and their numerical values, am I correct? So, I would first just grab a list of strings excluding Default which is quite simple. Then iterate through the string list, and add to a list of your SelectListItem class objects.

var res = Enum.GetNames(typeof(Fruits))
    .Where(x => x != "Default");

List<SelectListItem> list = new List<SelectListItem>();
foreach (string fruit in res)
{
    string desc = EnumHelper<Fruits>.GetEnumDescription(fruit); // This is a utility method I use. You can get the description using your extension method.
    int val = (int)Enum.Parse(typeof(Fruits), fruit);
    list.Add(new SelectListItem { enumName = fruit, enumDesc = desc, enumVal = val });
}

And if you must have that OrderBy, after creating your SelectListItem list, sort it once again by Description.

这篇关于c#枚举名称在DropDownList而不是值进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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