Example of Remote Data in ASP.NET MVC Tree Grid Control
This example demonstrates how to load child records on demand in the Tree Grid when using remote data. During the initial render, only parent rows are loaded and displayed in a collapsed state. Child records are fetched dynamically when a parent row is expanded.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
DataUrl = Url.Action("TreeUrlRoot", "TreeGridDemo"),
ChildrenUrl = Url.Action("TreeUrlChildren", "TreeGridDemo"),
LoadChildOnDemand = true,
EnableVirtualization = true,
IdMapping = "TaskID",
ParentIdMapping = "ParentValue",
HasChildMapping = "isParent",
TreeColumnIndex = 1,
Height = "450px",
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "TaskID", HeaderText = "Task ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 90, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "TaskName", HeaderText = "Task Name", Width = 150 },
new UiGridColumn { Field = "StartDate", HeaderText = "Start Date", Type = UiColumnType.Date, Format = "M/d/yyyy", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "EndDate", HeaderText = "End Date", Type = UiColumnType.Date, Format = "M/d/yyyy", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Duration", HeaderText = "Duration", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Progress", HeaderText = "Progress", Type = UiColumnType.Number, Width = 110 },
new UiGridColumn { Field = "Priority", HeaderText = "Priority", Width = 130 }
}
})
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
DataUrl = Url.Action("TreeUrlRoot", "TreeGridDemo"),
ChildrenUrl = Url.Action("TreeUrlChildren", "TreeGridDemo"),
LoadChildOnDemand = true,
EnableVirtualization = true,
IdMapping = "TaskID",
ParentIdMapping = "ParentValue",
HasChildMapping = "isParent",
TreeColumnIndex = 1,
Height = "450px",
Selection = UiGridSelection.Single,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "TaskID", HeaderText = "Task ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 90, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "TaskName", HeaderText = "Task Name", Width = 150 },
new UiGridColumn { Field = "StartDate", HeaderText = "Start Date", Type = UiColumnType.Date, Format = "M/d/yyyy", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "EndDate", HeaderText = "End Date", Type = UiColumnType.Date, Format = "M/d/yyyy", Width = 120, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Duration", HeaderText = "Duration", Type = UiColumnType.Number, Width = 110, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "Progress", HeaderText = "Progress", Type = UiColumnType.Number, Width = 110 },
new UiGridColumn { Field = "Priority", HeaderText = "Priority", Width = 130 }
}
})
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 TreeGridController : Controller
{
// The page is Views/TreeGrid/RemoteData.cshtml; it draws what ViewData holds.
public ActionResult RemoteData()
{
return View();
}
/// <summary>A task as Syncfusion's TreeUrlDataSource service sends it (what its remote data sample reads): ParentValue names the parent, isParent says there are sub-tasks.</summary>
public class TreeUrlTask
{
public int TaskID { get; set; }
public string TaskName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int Duration { get; set; }
public int Progress { get; set; }
public string Priority { get; set; }
public int? ParentValue { get; set; }
public bool isParent { get; set; }
}
/// <summary>5,000 parent tasks with 1 to 5 sub-tasks each (about 15,000 tasks), made the same way every time.</summary>
private static List<TreeUrlTask> TreeUrlTasks()
{
var tasks = new List<TreeUrlTask>();
string[] priorities = { "Critical", "High", "Normal", "Low" };
int id = 0;
for (int parent = 1; parent <= 5000; parent++)
{
int parentId = ++id;
int children = 1 + parent % 5;
DateTime start = new DateTime(2024, 1, 1).AddDays(parent % 300);
tasks.Add(new TreeUrlTask { TaskID = parentId, TaskName = "Parent Task " + parent, StartDate = start, EndDate = start.AddDays(30), Duration = 30, Progress = parent % 100, Priority = priorities[parent % 4], ParentValue = null, isParent = true });
for (int child = 1; child <= children; child++)
{
tasks.Add(new TreeUrlTask { TaskID = ++id, TaskName = "Child task " + parent + "." + child, StartDate = start.AddDays(child), EndDate = start.AddDays(child + 5), Duration = 5, Progress = (parent * child) % 100, Priority = priorities[(parent + child) % 4], ParentValue = parentId, isParent = false });
}
}
return tasks;
}
/// <summary>The parent tasks only (the remote sample's DataUrl): the children come when a parent opens.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult TreeUrlRoot()
{
return Json(TreeUrlTasks().Where(t => t.ParentValue == null).ToList());
}
/// <summary>The sub-tasks of one task: the grid posts { parentId } when the task first opens (its ChildrenUrl).</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult TreeUrlChildren(int? parentId)
{
return Json(TreeUrlTasks().Where(t => t.ParentValue == parentId).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>
@* Tree Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@