Selection in ASP.NET MVC ListBox Control

21 Dec 20222 minutes to read

The ListBox provides support to select an item or a group of item by mouse or keyboard action. There are two selection modes available in list box,

  • Single - To select single item in the list box.
  • Multiple - To select multiple items in the list box.

On selection of each list box item, change event is triggered.

Single selection

To enable single selection in the list box, mode should be set as Single in SelectionSettings property.

@Html.EJS().ListBox("listbox").DataSource((IEnumerable<object>)ViewBag.data).SelectionSettings(new Syncfusion.EJ2.DropDowns.ListBoxSelectionSettings { Mode = Syncfusion.EJ2.DropDowns.SelectionMode.Single}).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult selection()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}

Multiple selection

To enable multiple selection in the list box, mode should be set as Multiple in selectionSettings property.

To select multiple items, use the SHIFT, CTRL, and arrow keys to make selections.

NOTE

By default, the selection mode is set as Multiple.

@Html.EJS().ListBox("listbox").DataSource((IEnumerable<object>)ViewBag.data).SelectionSettings(new Syncfusion.EJ2.DropDowns.ListBoxSelectionSettings { Mode = Syncfusion.EJ2.DropDowns.SelectionMode.Multiple}).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class ListBoxController : Controller
    {
        public IActionResult selection()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}

NOTE

To select all the items in the list box, ShowSelectAll method can also be used.