# 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](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/treelist/binding/binding) 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 the `OnPostRead` or `OnGetRead` handler method in the `Index.cshtml.cs` file.

* Using a query string

  ```C#
  Url("/PathToPage?handler=HandlerName")
  ```

  For example, `Url("/Index?handler=Read")` references the `OnPostRead` or `OnGetRead` handler method in the `Index` page.

For more information on Razor Pages architecture and concepts, refer to [the official Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/).

## 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:

1. Configure the `Create`, `Read`, `Update`, and `Delete` methods of the `DataSource` instance. The URL in each option must refer to the method name in the `PageModel`. Also, set the `Id` field in the `Model()` configuration of the `DataSource`. It is mandatory for the `Create`, `Update`, `Delete` operations.

   ```Razor HtmlHelper
       @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)
       )
   ```

   ```Razor TagHelper
       @page
       @model IndexModel
       @using Kendo.Mvc.UI

       <kendo-treelist name="treelist" height="540">
           <toolbar>
               <treelist-toolbar-button name="create" />
           </toolbar>
           <columns>
               <treelist-column field="FirstName" width="220px" title="First Name"></treelist-column>
               <treelist-column field="LastName" title="Last Name" width="200px"></treelist-column>
               <treelist-column field="Position"></treelist-column>
               <treelist-column width="350px">
                   <commands>
                       <treelist-column-command name="createChild" text="Add child" />
                       <treelist-column-command name="edit" />
                       <treelist-column-command name="destroy" />
                   </commands>
               </treelist-column>
           </columns>
           <editable enabled="true"/>
           <treelist-datasource>
               <transport>
                   <read url="@Url.Page("Index", "Read")" data="forgeryToken"/>
                   <update url="@Url.Page("Index", "Update")" data="forgeryToken"/>
                   <create url="@Url.Page("Index", "Create")" data="forgeryToken"/>
                   <destroy url="@Url.Page("Index", "Destroy")" data="forgeryToken"/>
               </transport>
               <schema data="Data" total="Total" errors="Errors">
                   <treelist-model id="EmployeeId" parent-id="ReportsTo" expanded="true">
                       <fields>
                           <field name="EmployeeId" type="number"></field>
                           <field name="ReportsTo" nullable="true"></field>
                           <field name="FirstName" type="string"></field>
                           <field name="LastName" type="string"></field>
                           <field name="Position" type="string"></field>
                       </fields>
                   </treelist-model>
               </schema>
           </treelist-datasource>
       </kendo-treelist>
   ```

2. Add an `AntiForgeryToken` at the top of the page.

   ```Razor
       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
   ```

3. Send the `AntiForgeryToken` with 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>
   ```

4. Within the `cshtml.cs` file, add a handler method for each data operation.

   ```C# Index.cshtml.cs
   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));
       }
   }
   ```

   ```C# Model
       public class EmployeeDirectoryModel
       {
           [ScaffoldColumn(false)]
           public int EmployeeId { get; set; }

           [Required]
           [Display(Name = "First name")]
           public string FirstName { get; set; }

           [Display(Name = "Last name")]
           public string LastName { get; set; }

           [ScaffoldColumn(false)]
           public int? ReportsTo { get; set; }

           public string Position { get; set; }

           [ScaffoldColumn(false)]
           public bool hasChildren { get; set; }
       }
   ```

For the complete project, refer to the [TreeList in Razor Pages example](https://github.com/telerik/ui-for-aspnet-core-examples/blob/master/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml).

## See Also

* [Using Telerik UI for ASP.NET Core in Razor Pages](https://docs.telerik.com/aspnet-core/getting-started/razor-pages#using-telerik-ui-for-aspnet-core-in-razor-pages)
* [Client-Side API of the TreeList](https://docs.telerik.com/kendo-ui/api/ui/treelist)
* [Server-Side HtmlHelper API of the TreeList](https://www.telerik.com/aspnet-core-ui/documentation/api/treelist)
* [Server-Side TagHelper API of the TreeList](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/treelist)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
