Example of Local Data in ASP.NET MVC DropDownTree Control
This sample explains you about the different local data binding supports of the Dropdown Tree control. Click the Dropdown Tree element, and then select an item from the hierarchical structure suggestion list.
@Html.UiDropDownTree("ddtlocal", new UiDropDownTreeOptions
{
Placeholder = "Select a item",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.DropDownTreeHierarchical>)ViewData["hierarchical"],
Fields = new UiDropDownFields { Value = "Code", Text = "Name", Child = "Child" }
})
@Html.UiDropDownTree("ddtlist", new UiDropDownTreeOptions
{
Placeholder = "Select a item",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.DropDownTreeLocalData>)ViewData["local"],
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>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="content" style="padding-top:15px">
<h4> Hierarchical Data</h4>
@Html.UiDropDownTree("ddtlocal", new UiDropDownTreeOptions
{
Placeholder = "Select a item",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.DropDownTreeHierarchical>)ViewData["hierarchical"],
Fields = new UiDropDownFields { Value = "Code", Text = "Name", Child = "Child" }
})
<h4 style="margin-top:24px">List Data</h4>
@Html.UiDropDownTree("ddtlist", new UiDropDownTreeOptions
{
Placeholder = "Select a item",
PopupHeight = "200px",
DataSource = (IEnumerable<DropdownsDemoController.DropDownTreeLocalData>)ViewData["local"],
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/LocalData.cshtml; it draws what ViewData holds.
public ActionResult LocalData()
{
ViewData["local"] = new DropDownTreeLocalData().getDropDownTreeLocalData();
ViewData["hierarchical"] = new DropDownTreeHierarchical().getDropDownTreeHierarchical();
return View();
}
public class DropDownTreeLocalData
{
public int Id { get; set; }
public int? PId { get; set; }
public string Name { get; set; }
public bool HasChild { get; set; }
public bool Expanded { get; set; }
public List<DropDownTreeLocalData> getDropDownTreeLocalData()
{
List<DropDownTreeLocalData> localData = new List<DropDownTreeLocalData>();
localData.Add(new DropDownTreeLocalData { Id = 1, Name = "Discover Music", HasChild = true, Expanded = true });
localData.Add(new DropDownTreeLocalData { Id = 2, PId = 1, Name = "Hot Singles" });
localData.Add(new DropDownTreeLocalData { Id = 3, PId = 1, Name = "Rising Artists" });
// ... 24 more rows
return localData;
}
}
public class DropDownTreeHierarchical
{
public string Code { get; set; }
public string Name { get; set; }
public bool Expanded { get; set; }
public List<Countries2> Child { get; set; }
public List<DropDownTreeHierarchical> getDropDownTreeHierarchical()
{
List<DropDownTreeHierarchical> DropDownTreeHierarchical = new List<DropDownTreeHierarchical>();
List<Countries2> Countries1 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "NA",
Name = "North America",
Expanded = true,
Child = Countries1,
});
Countries1.Add(new Countries2 { Code = "USA", Name = "United States of America"});
Countries1.Add(new Countries2 { Code = "CUB", Name = "Cuba" });
Countries1.Add(new Countries2 { Code = "MEX", Name = "Mexico" });
List<Countries2> Countries2 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "AF",
Name = "Africa",
Child = Countries2,
});
Countries2.Add(new Countries2 { Code = "NGA", Name = "Nygeria" });
Countries2.Add(new Countries2 { Code = "EGY", Name = "Egypt" });
Countries2.Add(new Countries2 { Code = "ZAF", Name = "South Africa" });
List<Countries2> Countries3 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "AS",
Name = "Asia",
Child = Countries3,
});
Countries3.Add(new Countries2 { Code = "CHN", Name = "China" });
Countries3.Add(new Countries2 { Code = "IND", Name = "India" });
Countries3.Add(new Countries2 { Code = "JPN", Name = "Japan" });
List<Countries2> Countries4 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "EU",
Name = "Europe",
Child = Countries4,
});
Countries4.Add(new Countries2 { Code = "DNK", Name = "Denmark" });
Countries4.Add(new Countries2 { Code = "FIN", Name = "Finland" });
Countries4.Add(new Countries2 { Code = "AUT", Name = "Austria" });
List<Countries2> Countries5 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "SA",
Name = "South America",
Child = Countries5,
});
Countries5.Add(new Countries2 { Code = "BRA", Name = "Brazil" });
Countries5.Add(new Countries2 { Code = "COL", Name = "Colombia" });
Countries5.Add(new Countries2 { Code = "ARG", Name = "Argentina" });
List<Countries2> Countries6 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "OC",
Name = "Oceania",
Child = Countries6,
});
Countries6.Add(new Countries2 { Code = "AUS", Name = "Australia" });
Countries6.Add(new Countries2 { Code = "NZL", Name = "Newzealand" });
Countries6.Add(new Countries2 { Code = "WSM", Name = "Samoa" });
List<Countries2> Countries7 = new List<Countries2>();
DropDownTreeHierarchical.Add(new DropDownTreeHierarchical
{
Code = "AN",
Name = "Antartica",
Child = Countries7,
});
Countries7.Add(new Countries2 { Code = "BVT", Name = "Bouvet Island" });
Countries7.Add(new Countries2 { Code = "ATF", Name = "French Southern Lands" });
return DropDownTreeHierarchical;
}
}
}
}
@* 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 loads the data through the DataSource property, where the data can be either local data or remote data. In case of local data, the data structure can be hierarchical data or list data (with self-referential format i.e., mapped with the Value and ParentValue fields).
In this demo, the first Dropdown Tree is bound with the hierarchical data that contains the array of nested objects. And, the second Dropdown Tree is bound with the list type data where the parent-child relation is referred by the Value and ParentValue mapping fields.