Example of Checkbox in ASP.NET MVC ListBox Control
This sample demonstrates the checkbox functionalities of the ListBox. Click one or more items from the list of items in the ListBox.
Select your favorite car:
- Hennessey Venom
- Bugatti Chiron
- Bugatti Veyron Super Sport
- SSC Ultimate Aero
- Koenigsegg CCR
- McLaren F1
- Aston Martin One- 77
- Jaguar XJ220
- McLaren P1
- Ferrari LaFerrari
@Html.UiListBox("multi-select-listbox", new UiListBoxOptions { ShowCheckbox = true, DataSource = (IEnumerable<object>)ViewData["data"], Fields = new UiDropDownFields { Value = "id", Text = "text" } })
<style>
.control-section #listbox-control, .control-section #listbox-selection { margin: 0 auto; width: 300px; padding-top: 30px; }
.control-section h4 { margin: 0 0 8px; }
</style>
<div class="control-section">
<div id="listbox-selection">
<h4>Select your favorite car:</h4>
@Html.UiListBox("multi-select-listbox", new UiListBoxOptions { ShowCheckbox = true, DataSource = (IEnumerable<object>)ViewData["data"], Fields = new UiDropDownFields { Value = "id", Text = "text" } })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListBoxController : Controller
{
// The page is Views/ListBox/Checkbox.cshtml; it draws what ViewData holds.
public ActionResult Checkbox()
{
List<object> data = new List<object>();
data.Add(new { text = "Hennessey Venom", id = "list-01" });
data.Add(new { text = "Bugatti Chiron", id = "list-02" });
data.Add(new { text = "Bugatti Veyron Super Sport", id = "list-03" });
data.Add(new { text = "SSC Ultimate Aero", id = "list-04" });
data.Add(new { text = "Koenigsegg CCR", id = "list-05" });
data.Add(new { text = "McLaren F1", id = "list-06" });
data.Add(new { text = "Aston Martin One- 77", id = "list-07" });
data.Add(new { text = "Jaguar XJ220", id = "list-08" });
data.Add(new { text = "McLaren P1", id = "list-09" });
data.Add(new { text = "Ferrari LaFerrari", id = "list-10" });
ViewData["data"] = data;
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The ListBox component has built-in support to select multiple items from the list. The Checkbox selection can be enabled by setting the showCheckbox as true in the SelectionSettings property.
To perform the checkbox feature in the ListBox, the CheckBoxSelection module has to be injected at the application level.