ASP.NET MVC ListBox - Set Selected Items


  SelectedValue="ANATR,ALFKI,AROUT"
  • 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
  • B's BeveragesBSBEV
  • Cactus Comidas para llevarCACTU
  • Centro comercial MoctezumaCENTC
  • Chop-suey ChineseCHOPS
  • Comércio MineiroCOMMI
  • Consolidated HoldingsCONSH
  • Die Wandernde KuhWANDK
  • Drachenblut DelikatessenDRACD
  • Du monde entierDUMON
  • Eastern ConnectionEASTC
  • Ernst HandelERNSH
  • Familia ArquibaldoFAMIA
  • FISSA Fabrica Inter. Salchichas S.A.FISSA
  • Folies gourmandesFOLIG
  • Folk och fä HBFOLKO
  Item.Selected = true;
  • 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
  • B's BeveragesBSBEV
  • Cactus Comidas para llevarCACTU
  • Centro comercial MoctezumaCENTC
  • Chop-suey ChineseCHOPS
  • Comércio MineiroCOMMI
  • Consolidated HoldingsCONSH
  • Die Wandernde KuhWANDK
  • Drachenblut DelikatessenDRACD
  • Du monde entierDUMON
  • Eastern ConnectionEASTC
  • Ernst HandelERNSH
  • Familia ArquibaldoFAMIA
  • FISSA Fabrica Inter. Salchichas S.A.FISSA
  • Folies gourmandesFOLIG
  • Folk och fä HBFOLKO
The ListBox control provides multiple ways to set the initial selected items (for multi-item selection):
  • SelectedValue - the values of the items to be selected (the values need to be separated
    using the character specified in the MultiSelectionSeparator property - comma by default).
  • Selected - this is a property of the ListBoxItem class. If set to true, the item will be selected.
View - Aspx

    <% Html.Obout(new ListBox("ListBox1")
    {   
        SelectedValue = "ANATR,ALFKI,AROUT",
        SelectionMode = ListBoxSelectionMode.Multiple
    }); %> 

    <% Html.Obout(new ListBox("ListBox2")
    {   
        SelectionMode = ListBoxSelectionMode.Multiple
    }); %>  
    
    
View - Razor

    @Html.Obout(new ListBox("ListBox1")
    {   
        SelectedValue = "ANATR,ALFKI,AROUT",
        SelectionMode = ListBoxSelectionMode.Multiple
    })

    @Html.Obout(new ListBox("ListBox2")
    {   
        SelectionMode = ListBoxSelectionMode.Multiple
    })
    
    
Controller

    public ViewResult SelectionMultiSet()
        {
            ViewData["ListBox1"] = GetCustomers();

            IEnumerable<ListBoxItem> dbItems = GetCustomers().Take(25);
            List<ListBoxItem> items = new List<ListBoxItem>();
            foreach (ListBoxItem tempItem in dbItems)
            {
                if (tempItem.Value == "AROUT" || tempItem.Value == "BLONP" || tempItem.Value == "BOLID")
                {
                    tempItem.Selected = true;
                }

                items.Add(tempItem);
            }

            ViewData["ListBox2"] = items;

            return View();
        }