C# 如何在下拉列表中使用C枚举

C# 如何在下拉列表中使用C枚举,c#,listview,drop-down-menu,enums,webforms,C#,Listview,Drop Down Menu,Enums,Webforms,我使用EntityFramework5创建了一个数据库,并使用了enum功能。现在我想使用我在下拉列表中定义的这些枚举 我的枚举是由EF在此处创建的: namespace Dumpling { using System; public enum DebtType : short { Mortgage = 0, Installment = 1, Revolving = 2, Judgement = 3,

我使用EntityFramework5创建了一个数据库,并使用了enum功能。现在我想使用我在下拉列表中定义的这些枚举

我的枚举是由EF在此处创建的:

namespace Dumpling
{
    using System;

    public enum DebtType : short
    {
        Mortgage = 0,
        Installment = 1,
        Revolving = 2,
        Judgement = 3,
        TaxLien = 4,
        TradelineDispute = 5,
        AddressDiscrepancy = 6, 
        NameVariation = 7
    }
}

我希望在ListView中创建一个下拉列表。我不确定如何获取下拉列表datasourceID以使用枚举。我希望通过什么来实现这一点?

来自NopCommerce代码库,该代码库使用稍微修改过的MVC:

public static SelectList ToSelectList<TEnum>(this TEnum enumObj, bool markCurrentAsSelected = true) where TEnum : struct {
    if (!typeof(TEnum).IsEnum) throw new ArgumentException("An Enumeration type is required.", "enumObj");

    var values = from TEnum enumValue in Enum.GetValues(typeof(TEnum))
                    select new { ID = Convert.ToInt32(enumValue), Name = enumValue.ToString() };
    object selectedValue = null;
    if (markCurrentAsSelected)
        selectedValue = Convert.ToInt32(enumObj);
    return new SelectList(values, "ID", "Name", selectedValue);
}

我还没有对其进行过测试,只是做了一些小的修改,删除了一些特定于NC的代码,但是基本概念应该可以帮助您实现这一点。当然,你不会有SelectList,但你应该能够很容易地修改它。

好吧,如果你在ListView中有DropDownList,那么我会使用以下方法,它可能会被优化,我的ASP.NET Web表单技能正在生锈

型号:

public enum AnimalType
{
    Dog = 1,
    Cat = 2,
    Sheep = 3,
    Horse = 4
}

public class Animal
{
    public string Name { get; set; }
    public AnimalType Type { get; set; }
}
第页:

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
   var animals = new List<Animal>();
   animals.Add(new Animal() { Name = "Doggie", Type = AnimalType.Dog});
   animals.Add(new Animal() { Name = "Sheepie", Type = AnimalType.Sheep });
   lstAnimals.DataSource = animals;
   lstAnimals.DataBind();
}

protected void lstAnimals_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var ddlAnimalType = (DropDownList)e.Item.FindControl("lstAnimalType");
    var enumValues = Enum.GetValues(typeof (AnimalType)).Cast<AnimalType>().ToList();
    var bindableList = enumValues.Select(v => new { Id = (int) v, Description = v.ToString() });
    ddlAnimalType.DataSource = bindableList;
    ddlAnimalType.DataBind();
}

使用Enum.GetNames方法。谢谢,这很有效。当dropdownlist在ListView之外时,Enum.GetNames方法可以工作。ID在ListView中时不可见。你知道为什么吗?
protected void Page_Load(object sender, EventArgs e)
{
   var animals = new List<Animal>();
   animals.Add(new Animal() { Name = "Doggie", Type = AnimalType.Dog});
   animals.Add(new Animal() { Name = "Sheepie", Type = AnimalType.Sheep });
   lstAnimals.DataSource = animals;
   lstAnimals.DataBind();
}

protected void lstAnimals_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var ddlAnimalType = (DropDownList)e.Item.FindControl("lstAnimalType");
    var enumValues = Enum.GetValues(typeof (AnimalType)).Cast<AnimalType>().ToList();
    var bindableList = enumValues.Select(v => new { Id = (int) v, Description = v.ToString() });
    ddlAnimalType.DataSource = bindableList;
    ddlAnimalType.DataBind();
}