ASP.NET MVC ListBox - Populating with Data


Populated from database (LINQ to Entities):
  • Alfreds FutterkisteALFKI
  • Antonio Moreno TaqueríaANATR
  • Around the HornAROUT
  • Berglunds snabbköpANTON
  • Berglunds snabbköpBERGS
  • Blauer See DelikatessenBLAUS
  • Blondesddsl père et filsBLONP
  • Bólido Comidas preparadasBOLID
  • Bon app'BONAP
  • Bottom-Dollar MarketsBOTTM


Populated from generic collection of custom domain model objects:
  • Alfreds FutterkisteALFKI
  • Antonio Moreno TaqueríaANATR
  • Around the HornAROUT
  • Berglunds snabbköpANTON
  • Berglunds snabbköpBERGS
  • Blauer See DelikatessenBLAUS
  • Blondesddsl père et filsBLONP
  • Bólido Comidas preparadasBOLID
  • Bon app'BONAP
  • Bottom-Dollar MarketsBOTTM


Manually populated:
  • Alfreds FutterkisteALFKI
  • Antonio Moreno TaqueríaANATR
  • Around the HornAROUT
  • Berglunds snabbköpANTON
  • Berglunds snabbköpBERGS
  • Blauer See DelikatessenBLAUS
  • Blondesddsl père et filsBLONP
  • Bólido Comidas preparadasBOLID
  • Bon app'BONAP
  • Bottom-Dollar MarketsBOTTM
The ListBox can be populated in various ways:
- LINQ to Entities;
- LINQ to SQL;
- generic collections;
- manually;
- and many other.
View - Aspx

   <% Html.Obout(new ListBox("ListBox1")
        {
           Width = 250        
        }); %>
        
    <% Html.Obout(new ListBox("ListBox2")
        {
           Width = 250       
        }); %>
        
    <% Html.Obout(new ListBox("ListBox3")
        {
           Width = 250         
        }); %>
    
View - Razor

    @Html.Obout(new ListBox("ListBox1")
        {
           Width = 250        
        })
        
    @Html.Obout(new ListBox("ListBox2")
        {
           Width = 250       
        })
        
    @Html.Obout(new ListBox("ListBox3")
        {
           Width = 250         
        })
    
Controller

   public ViewResult PopulateWithData()
    {
        // Populate from database
        NorthwindEntities db = new NorthwindEntities();
        var customers = from c in db.Customers
                        select new
                        {
                            c.CustomerID,
                            c.CompanyName
                        };

        ListBoxItemList customersList = new ListBoxItemList(customers.Take(10), "CustomerID", "CompanyName");

        ViewData["ListBox1"] = customersList;

        // Populate from manually created list
        List items = new List();
        items.Add(new ListBoxItem() { Value = "1", Text = "Item 1" });
        items.Add(new ListBoxItem() { Value = "2", Text = "Item 2" });
        items.Add(new ListBoxItem() { Value = "3", Text = "Item 3" });
        items.Add(new ListBoxItem() { Value = "4", Text = "Item 4" });
        items.Add(new ListBoxItem() { Value = "5", Text = "Item 5" });

        ViewData["ListBox3"] = items;

        // Populate from generic collection of custom objects (Domain Model classes)
        List products = new List();
        products.Add(new Product() { ProductID = 1, ProductName = "Product 1" });
        products.Add(new Product() { ProductID = 2, ProductName = "Product 2" });
        products.Add(new Product() { ProductID = 3, ProductName = "Product 3" });
        products.Add(new Product() { ProductID = 4, ProductName = "Product 4" });
        products.Add(new Product() { ProductID = 5, ProductName = "Product 5" });

        ViewData["ListBox2"] = new ListBoxItemList(products, "ProductID", "ProductName");

        return View();
    }