Example of Checkbox in ASP.NET MVC DropDownTree Control
This sample explains you about the CheckBox functionalities of the Dropdown Tree. Click on any parent item's CheckBox to check or uncheck the item and its child items. The parent item's checked state will be determined by its child item’s checked state.
@Html.UiDropDownTree("ddt", new UiDropDownTreeOptions
{
Placeholder = "Select items",
Mode = UiMultiSelectMode.Delimiter,
ShowCheckBox = true,
AutoCheck = false,
PopupHeight = "200px",
DataSource = (IEnumerable<object>)ViewData["data"],
Fields = new UiDropDownFields { Value = "id", Text = "name", ParentValue = "pid", Expanded = "expanded" }
})
@Html.UiCheckBox("select", false, new UiCheckBoxOptions { Label = "Auto Check", OnChange = "onChange" })
<style>
.control-section .content { margin: 0 auto; width: 250px; }
.control-section h4 { margin: 0 0 8px; }
</style>
<div class="control-section">
<div class="content" style="padding-top:75px">
@Html.UiDropDownTree("ddt", new UiDropDownTreeOptions
{
Placeholder = "Select items",
Mode = UiMultiSelectMode.Delimiter,
ShowCheckBox = true,
AutoCheck = false,
PopupHeight = "200px",
DataSource = (IEnumerable<object>)ViewData["data"],
Fields = new UiDropDownFields { Value = "id", Text = "name", ParentValue = "pid", Expanded = "expanded" }
})
</div>
</div>
<div class="demo-property-panel" title="Properties">
<div class="demo-property-row">@Html.UiCheckBox("select", false, new UiCheckBoxOptions { Label = "Auto Check", OnChange = "onChange" })</div>
</div>
<script>
// Syncfusion: the check box sets treeSettings.autoCheck.
function onChange(checkedValue) { window.Lumen.get('ddt').setAutoCheck(checkedValue); }
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DropDownTreeController : Controller
{
// The page is Views/DropDownTree/Checkbox.cshtml; it draws what ViewData holds.
public ActionResult Checkbox()
{
ViewData["data"] = new DropDownTreeCheckbox().Checkbox();
return View();
}
public class DropDownTreeCheckbox
{
public List<Object> Checkbox()
{
List<Object> parentitem = new List<Object>();
parentitem.Add(new { id = 1, name = "Australia", hasChild = true, expanded = true });
parentitem.Add(new { id = 2, pid = 1, name = "New South Wales" });
parentitem.Add(new { id = 3, pid = 1, name = "Victoria" });
parentitem.Add(new { id = 4, pid = 1, name = "South Australia" });
parentitem.Add(new { id = 6, pid = 1, name = "Western Australia" });
parentitem.Add(new { id = 7, name = "Brazil", hasChild = true });
parentitem.Add(new { id = 8, pid = 7, name = "Paraná" });
parentitem.Add(new { id = 9, pid = 7, name = "Ceará" });
parentitem.Add(new { id = 10, pid = 7, name = "Acre" });
parentitem.Add(new { id = 11, name = "China", hasChild = true });
parentitem.Add(new { id = 12, pid = 11, name = "Guangzhou" });
parentitem.Add(new { id = 13, pid = 11, name = "Shanghai" });
parentitem.Add(new { id = 14, pid = 11, name = "Beijing" });
parentitem.Add(new { id = 15, pid = 11, name = "Shantou" });
parentitem.Add(new { id = 16, name = "France", hasChild = true });
parentitem.Add(new { id = 17, pid = 16, name = "Pays de la Loire" });
parentitem.Add(new { id = 18, pid = 16, name = "Aquitaine" });
parentitem.Add(new { id = 19, pid = 16, name = "Brittany" });
parentitem.Add(new { id = 20, pid = 16, name = "Lorraine" });
parentitem.Add(new { id = 21, name = "India", hasChild = true });
parentitem.Add(new { id = 22, pid = 21, name = "Assam" });
parentitem.Add(new { id = 24, pid = 21, name = "Tamil Nadu" });
parentitem.Add(new { id = 25, pid = 21, name = "Punjab" });
return parentitem;
}
}
}
}
@* 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 Dropdown Tree control can be rendered with the checkbox on the left side of each tree item. This allows the user to check more than one item, and this can be enabled by the ShowCheckBox property.
In this demo, the Dropdown Tree is populated with the checkbox enabled feature.