TreeList in Razor Pages
This article describes how to seamlessly integrate and configure the Telerik UI TreeList for ASP.NET Core in Razor Pages applications.
You can use any of the available data binding approaches to bind the component to data in a Razor Pages application.
Referencing Handler Methods in Razor Pages
Razor Pages is an alternative to the MVC pattern that makes page-focused coding easier and more productive. This approach consists of a cshtml file and a cshtml.cs file (by convention, the two files have the same name).
The cshtml.cs file, known as the PageModel, contains handler methods that respond to HTTP requests. These methods are prefixed with On followed by the HTTP verb (for example, OnGet, OnPost, OnPostRead, OnPostCreate).
Handler methods declared in a PageModel can be referenced from any Razor Page using one of the following URL patterns:
-
Using
Url.Page()C#Url.Page("PageName", "HandlerName") // OR Url.Page("/FolderName/PageName", "HandlerName")For example,
Url.Page("Index", "Read")references theOnPostReadorOnGetReadhandler method in theIndex.cshtml.csfile. -
Using a query string
C#Url("/PathToPage?handler=HandlerName")For example,
Url("/Index?handler=Read")references theOnPostReadorOnGetReadhandler method in theIndexpage.
For more information on Razor Pages architecture and concepts, refer to the official Microsoft documentation.
Binding to Remote Data
To bind the TreeList to a data set received from a remote endpoint within a Razor Pages application, follow the next steps:
-
Configure the
Create,Read,Update, andDeletemethods of theDataSourceinstance. The URL in each option must refer to the method name in thePageModel. Also, set theIdfield in theModel()configuration of theDataSource. It is mandatory for theCreate,Update,Deleteoperations.Razor@page @model IndexModel @using Kendo.Mvc.UI @(Html.Kendo().TreeList<EmployeeDirectoryModel>() .Name("treelist") .Toolbar(toolbar => toolbar.Create()) .Columns(columns => { columns.Add().Field(e => e.FirstName).Title("First Name").Width(220); columns.Add().Field(e => e.LastName).Title("Last Name").Width(200); columns.Add().Field(e => e.Position); columns.Add().Width(350).Command(c => { c.CreateChild().Text("Add child"); c.Edit(); c.Destroy(); }) .HtmlAttributes(new { style = "text-align: center;" }); }) .Editable() .DataSource(dataSource => dataSource .Read(r => r.Url(Url.Page("Index", "Read")).Data("forgeryToken")) .Update(u => u.Url(Url.Page("Index", "Update")).Data("forgeryToken")) .Create(c => c.Url(Url.Page("Index", "Create")).Data("forgeryToken")) .Destroy(d => d.Url(Url.Page("Index", "Destroy")).Data("forgeryToken")) .Model(m => { m.Id(f => f.EmployeeId); // Provide the Id property of the model. m.ParentId(f => f.ReportsTo); // Provide the Child Id property of the model. m.Expanded(true); // Set to "true" if you want the TreeList to be expanded by default. m.Field(f => f.FirstName); m.Field(f => f.LastName); m.Field(f => f.ReportsTo); m.Field(f => f.Position); }) ) .Height(540) ) -
Add an
AntiForgeryTokenat the top of the page.Razor@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf @Html.AntiForgeryToken() -
Send the
AntiForgeryTokenwith the CRUD requests.JavaScript<script> function forgeryToken() { return kendo.antiForgeryTokens(); } </script>Additional parameters can also be supplied.
JavaScript<script> function forgeryToken() { return { __RequestVerificationToken: kendo.antiForgeryTokens().__RequestVerificationToken, additionalParameter: "test" } } </script> -
Within the
cshtml.csfile, add a handler method for each data operation.C#public class IndexModel : PageModel { private static IList<EmployeeDirectoryModel> employees; public void OnGet() { GetDirectory(); } private IList<EmployeeDirectoryModel> GetDirectory() { if (employees == null) { // Populate the "employees" collection with data. employees = new List<EmployeeDirectoryModel>(); employees.Add(new EmployeeDirectoryModel { EmployeeId = 1, FirstName = "Daryl", LastName = "Sweeney", ReportsTo = null, Position = "CEO", }); employees.Add(new EmployeeDirectoryModel { EmployeeId = 2, FirstName = "Guy", LastName = "Wooten", ReportsTo = 1, Position = "Chief Technical Officer" }); ... // Add more records. } return employees; } public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) { var result = employees.ToTreeDataSourceResult(request, e => e.EmployeeId, e => e.ReportsTo, e => e ); return new JsonResult(result); } public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) { employee.EmployeeId = employees.Count + 2; employees.Add(employee); return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); } public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) { var target = employees.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId); if(target != null) { target.FirstName = employee.FirstName; target.LastName = employee.LastName; target.Position = employee.Position; target.ReportsTo = employee.ReportsTo; } return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); } public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) { employees.Remove(employees.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId)); return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); } }
For the complete project, refer to the TreeList in Razor Pages example.