Example of Remote Data in ASP.NET MVC TreeView Control
This ASP.NET MVC TreeView example demonstrates the binding data to the TreeView from remote data source. On expanding the parent node, the spinner icon will be displayed until the child nodes will be loaded into parent node. Click on node to select it, and click on icon or double click on node to expand/collapse it.
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", DataUrl = Url.Action("TreeEmployees", "NavigationDemo"), Fields = new UiTreeViewFields { Id = "Id", Text = "Name", HasChildren = "HasChild" } })
<style>
.control_wrapper { max-width: 500px; margin: auto; border: 1px solid #dddddd; border-radius: 3px; }
</style>
@Html.AntiForgeryToken()
<div class="control-section">
<div class="control_wrapper">
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree", DataUrl = Url.Action("TreeEmployees", "NavigationDemo"), Fields = new UiTreeViewFields { Id = "Id", Text = "Name", HasChildren = "HasChild" } })
</div>
</div>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeViewController : Controller
{
// The page is Views/TreeView/RemoteData.cshtml; it draws what ViewData holds.
public ActionResult RemoteData()
{
return View();
}
/// <summary>The DataUrl of the tree-remote sample: the children of a node ({ parentId }, null for the roots).</summary>
private static readonly Dictionary<string, string[]> Employees = new Dictionary<string, string[]>
{
{ "", new[] { "e1:Nancy", "e2:Andrew", "e3:Janet", "e4:Margaret", "e5:Steven" } },
{ "e1", new[] { "o10248:Vins et alcools Chevalier", "o10251:Victuailles en stock", "o10253:Hanari Carnes", "o10256:Wellington Importadora", "o10259:Centro comercial Moctezuma" } },
{ "e2", new[] { "o10249:Toms Spezialitäten", "o10252:Suprêmes délices", "o10254:Chop-suey Chinese", "o10257:HILARION-Abastos", "o10260:Ottilies Käseladen" } },
{ "e3", new[] { "o10255:Richter Supermarkt", "o10258:Ernst Handel", "o10261:Que Delícia", "o10262:Rattlesnake Canyon Grocery", "o10265:Blondel père et fils" } },
{ "e4", new[] { "o10250:Hanari Carnes", "o10263:Ernst Handel", "o10266:Wartian Herkku", "o10269:White Clover Markets", "o10272:Rattlesnake Canyon Grocery" } },
{ "e5", new[] { "o10264:Folk och fä HB", "o10267:Frankenversand", "o10270:Piccolo und mehr", "o10273:Queen Cozinha", "o10276:Tortuga Restaurante" } }
};
/// <summary>The DataUrl of the tree-view remote data sample (Syncfusion reads the Northwind OData service): the employees, and the orders of an employee when it opens.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult TreeEmployees(string parentId)
{
string[] children;
if (!Employees.TryGetValue(parentId ?? string.Empty, out children)) { children = new string[0]; }
return Json(children.Select(c => new { Id = c.Split(':')[0], Name = c.Split(':')[1], HasChild = Employees.ContainsKey(c.Split(':')[0]) }).ToList());
}
}
}
@* 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>
@* Navigations with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The TreeView component loads the data through the dataSource property, where the data can be either local data or remote data. In case of remote data, the data can be loaded from any remote services though the DataManager.
The DataManager will act as an interface between the service endpoint and the TreeView, that requires the below minimal information to interact with the service endpoint.
DataManager->url- Defines the service endpoint to fetch data.DataManager->adaptor- Defines the adaptor option. By default, ODataV4Adaptor is used for remote binding.
In this demo, the TreeView is bound with the dataSource from the Northwind remote service by using the DataManager instance.