Example of CheckBox in ASP.NET MVC TreeView Control
This ASP.NET MVC TreeView example demonstrates the CheckBox functionalities of the TreeView. Click on any parent node's CheckBox to check/uncheck the node and its child nodes. The parent node's checked state will be determined by its child nodes checked state.
- ASP.NET MVC Team
- Smith
- Johnson
- Anderson
- Windows Team
- Clark
- Wright
- Lopez
@Html.UiTreeView(new UiTreeViewOptions { Id = "checkbox", ShowCheckBox = true, Nodes = (List<UiTreeNode>)ViewData["checkboxfields"] })
@Html.UiCheckBox("select", true, new UiCheckBoxOptions { Id = "select", Label = "Auto Check", OnChange = "onChange" })
<style>
.control_wrapper { max-width: 500px; margin: auto; border: 1px solid #dddddd; border-radius: 3px; }
</style>
<div class="control-section">
<div class="control_wrapper">
@Html.UiTreeView(new UiTreeViewOptions { Id = "checkbox", ShowCheckBox = true, Nodes = (List<UiTreeNode>)ViewData["checkboxfields"] })
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
@Html.UiCheckBox("select", true, new UiCheckBoxOptions { Id = "select", Label = "Auto Check", OnChange = "onChange" })
</div>
<script>
// Syncfusion: Auto Check sets the autoCheck of the TreeView
function onChange(checkedNow) {
window.Lumen.get('checkbox').setAutoCheck(checkedNow);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeViewController : Controller
{
// The page is Views/TreeView/CheckBox.cshtml; it draws what ViewData holds.
public ActionResult CheckBox()
{
ViewData["checkboxfields"] = new List<UiTreeNode>
{
new UiTreeNode { Id = "t1", Text = "ASP.NET MVC Team", Expanded = true, Children =
{
new UiTreeNode { Id = "t2", Text = "Smith" },
new UiTreeNode { Id = "t3", Text = "Johnson", Selected = true },
new UiTreeNode { Id = "t4", Text = "Anderson" }
} },
new UiTreeNode { Id = "t5", Text = "Windows Team", Expanded = true, Children =
{
new UiTreeNode { Id = "t6", Text = "Clark" },
new UiTreeNode { Id = "t7", Text = "Wright" },
new UiTreeNode { Id = "t8", Text = "Lopez" }
} }
};
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 TreeView component can be rendered with checkbox on the left side of each tree node. This allows the user to check more than one nodes, and this can be enabled by the showCheckBox property.
In this demo, the TreeView is populated with checkbox enabled.