TreeList 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 design, the two files have the same name).
You can seamlessly integrate the Telerik UI TreeList for ASP.NET Core in Razor Pages applications.
This article describes how to configure the TreeList component in a Razor Pages scenario.
For the complete project, refer to the TreeList in Razor Pages example.
Getting Started
To configure the CRUD operations of the TreeList DataSource 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("/Index?handler=Read").Data("forgeryToken")) .Update(u => u.Url("/Index?handler=Update").Data("forgeryToken")) .Create(c => c.Url("/Index?handler=Create").Data("forgeryToken")) .Destroy(d => d.Url("/Index?handler=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#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)); }