Local Binding
Local data binding refers to binding the DropDownTree component to a dataset that is prepared on the server and made available during the initial page rendering. The data is retrieved server-side (from a database, service, or other data source) and then passed to the view, where the BindTo() method accepts the IEnumerable collection.
The local data binding approach is often used when dealing with small to medium-sized datasets since all records are available when the page is loaded. Also, when the client-side filtering of the DropDownTree is enabled and the complete dataset can be accessed on the client, no additional server requests are needed, which provides fast and responsive user interactions. However, for large datasets, consider using Ajax data binding to avoid increased initial page load times and memory usage.
As of the R1 2024, the DropDownTree TagHelper will require explicit supplementation to a collection of type
DropDownTreeItemModelinstead of theDropDownTreeItemBase.
Configuration
To configure the DropDownTree for local data binding, follow the next steps:
-
Pass the data collection to the view through
ViewData.C#public ActionResult Index() { ViewBag.dropdowntreeData = GetData(); return View(); } private IEnumerable<DropDownTreeItemModel> GetData() { List<DropDownTreeItemModel> data = new List<DropDownTreeItemModel> { new DropDownTreeItemModel { Text = "Furniture", Items = new List<DropDownTreeItemModel> { new DropDownTreeItemModel() { Text = "Tables & Chairs" }, new DropDownTreeItemModel { Text = "Sofas" }, new DropDownTreeItemModel { Text = "Occasional Furniture" } } }, new DropDownTreeItemModel { Text = "Decor", Items = new List<DropDownTreeItemModel> { new DropDownTreeItemModel() { Text = "Bed Linen" }, new DropDownTreeItemModel { Text = "Curtains & Blinds" }, new DropDownTreeItemModel { Text = "Carpets" } } } }; return data; } -
Add the DropDownTree to the view and bind it to the data that is saved in the
ViewData.Razor@using Kendo.Mvc.UI.Fluent @(Html.Kendo().DropDownTree() .Name("dropdowntree") .BindTo((IEnumerable<DropDownTreeItemModel>)ViewBag.dropdowntreeData) )
Mapping Objects to DropDownTreeItemModels
When using local binding, map a custom model collection to a collection of DropDownTreeItemModel by using the BindTo() overload. By default, the items are serialized with the default JSON configuration of the DropDownTree items. When an initial value is set or the DropDownTreeFor(m=>m) overload is used and the DataTextField and DataValueField of the mapped objects differ from the default text and value fields, configure the DataTextField and DataValueField options to ensure proper model binding.
@using Kendo.Mvc.UI.Fluent
@model UserViewModel
@(Html.Kendo().DropDownTreeFor(m => m.SelectedOptions)
.Checkboxes(true)
.DataTextField("OptionName")
.DataValueField("OptionValue")
.AutoBind(true)
.BindTo((IEnumerable<CategoryItem>)ViewBag.DropDownTreeData, (NavigationBindingFactory<DropDownTreeItem> mappings) =>
{
mappings.For<CategoryItem>(binding => binding.ItemDataBound((item, option) =>
{
item.Text = option.OptionName;
item.Value = option.OptionValue;
})
.Children(category => category.Items));
})
)