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
This article showcases how to perform CRUD operations with the Telerik UI for ASP.NET CoreTreeList component in a Razor Pages scenario.
To set up the TreeList component bindings:
- Configure the
Create
,Read
,Update
,Delete
methods of theDataSource
instance. The URLs in these methods must refer to the method names in the PageModel. - Set the
Id
field in theModel
of theDataSource
. It is mandatory for theCreate
,Update
,Delete
operations.
See the implementation details in the example below. For the complete project with Razor Pages examples, visit our GitHub repository.
@page
@model Telerik.Examples.RazorPages.Pages.TreeList.TreeListCrudOperationsModel
@{
ViewData["Title"] = "TreeListCrudOperations";
}
@using Telerik.Examples.RazorPages.Models
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
@(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().Field(e => e.HireDate).Format("{0:MMMM d, yyyy}");
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("/TreeList/TreeListCrudOperations?handler=Read").Data("forgeryToken")) // Specify the url to the OnPostRead method.
.Update(u => u.Url("/TreeList/TreeListCrudOperations?handler=Update").Data("forgeryToken"))
.Create(c => c.Url("/TreeList/TreeListCrudOperations?handler=Create").Data("forgeryToken"))
.Destroy(d => d.Url("/TreeList/TreeListCrudOperations?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.HireDate);
m.Field(f => f.Position);
})
)
.Height(540)
)
<script>
function forgeryToken() {
return kendo.antiForgeryTokens();
}
</script>
<style>
.k-treelist .k-command-cell .k-button {
min-width: 0px;
padding: 10px 10px 10px 10px;
}
</style>