3

I created a database using Entity Framework 5 and used the enum feature. I now want to use these enums I defined in a drop down list.

My enum was created by EF here:

namespace Dumpling
{
    using System;

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

What I hope to do is make a dropdown list inside a ListView. I'm not certain how to get the dropdown list datasourceID to use the enum. What would my <asp:DropDownList> look like to accomplish this?

2
  • Use Enum.GetNames Method. Commented Dec 30, 2012 at 15:23
  • Thanks, that sort of worked. The Enum.GetNames method works when the dropdownlist is outside the ListView. The ID is not visible when it is inside the ListView. Any thoughts why? Commented Dec 31, 2012 at 1:57

2 Answers 2

3

From the NopCommerce code base, which uses MVC (slightly modified):

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);
}

Haven't tested this with the minor modifications to remove some NC-specific code, but the basic concept should get you there. Of course you won't have SelectList, but you should be able to modify this pretty easily.

Sign up to request clarification or add additional context in comments.

Comments

1

Well, if you have your DropDownList inside the ListView, then I would use following approach (it probably can be optimized, my ASP.NET Web Forms skills are getting rusty).

Model:

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

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

Page:

<asp:ListView ID="lstAnimals" runat="server" onitemdatabound="lstAnimals_ItemDataBound">    
<ItemTemplate>
    <div>
        <asp:TextBox runat="server" Text='<%#Eval("Name") %>' />
        <asp:DropDownList ID="lstAnimalType" runat="server" DataValueField="Id" DataTextField="Description"  />
    </div>
</ItemTemplate>   

Code Behind:

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.