Example of Custom Template in ASP.NET MVC DropDownTree Control
This sample explains you about the custom template support of the Dropdown Tree. When you click the checkbox, the selected items will be visualized based on the given custom template.
@Html.UiDropDownTree("ddt", new UiDropDownTreeOptions
{
Placeholder = "Select items",
Mode = UiMultiSelectMode.Custom,
CustomTemplate = "${value.length} item(s) selected",
ShowCheckBox = true,
AutoCheck = true,
PopupHeight = "200px",
DataSource = (IEnumerable<object>)ViewData["data"],
Fields = new UiDropDownFields { Value = "id", Text = "name", ParentValue = "pid", Expanded = "expanded" }
})
<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.Custom,
CustomTemplate = "${value.length} item(s) selected",
ShowCheckBox = true,
AutoCheck = true,
PopupHeight = "200px",
DataSource = (IEnumerable<object>)ViewData["data"],
Fields = new UiDropDownFields { Value = "id", Text = "name", ParentValue = "pid", Expanded = "expanded" }
})
</div>
</div>
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/CustomTemplate.cshtml; it draws what ViewData holds.
public ActionResult CustomTemplate()
{
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 allows the user to select more than one value while enabling the checkbox or multi selection support.
The user can visualize the custom template instead of the selected item texts in the Dropdown Tree by enabling the mode type as Custom. User can also customize the given template by using the customTemplate property.
In this demo, the Dropdown Tree is populated with the checkbox and custom template feature.