# DropDownTree in Razor Pages

This article describes how to seamlessly integrate and configure the Telerik UI DropDownTree 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/editors/dropdowntree/binding/binding-overview#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 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

The most flexible form of data binding is to use the [DataSource](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/datasource/overview) component. To bind the DropDownTree to a data set received from a remote endpoint within a Razor Pages application, follow the next steps:

1. Specify the Read request URL in the `DataSource` configuration. The URL must refer to the method name in the `PageModel`.

   ```Razor HtmlHelper
       @page
       @model DropDownTreeIndexModel

       @(Html.Kendo().DropDownTree()		
           .Name("dropdowntree")
           .AutoWidth(true)
           .DataTextField("Name")
           .HtmlAttributes(new { style = "width: 100%" })
           .CheckAll(true)
           .AutoClose(false)
           .Checkboxes(checkboxes => checkboxes
               .CheckChildren(true)
           )
           .DataSource(dataSource => dataSource
               .Custom()
               .Transport(t => t
                   .Read(r => r.Url(Url.Page("DropDownTreeIndex", "DropDownTreeRead")).Data("forgeryToken")))
               )
       )
   ```

   ```Razor TagHelper
       @page
       @model DropDownTreeIndexModel

       <kendo-dropdowntree name="dropdowntree" style="width: 100%"
           datatextfield="Name" 
           auto-close="false"
           auto-width="true">
           <hierarchical-datasource>
               <schema>
                   <hierarchical-model id="id"></hierarchical-model>
               </schema>
               <transport>
                   <read url="@Url.Page("DropDownTreeIndex", "DropDownTreeRead")" data="forgeryToken" />
               </transport>
           </hierarchical-datasource>
           <checkboxes check-children="true" enabled="true" />
       </kendo-dropdowntree>
   ```

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 Read request.

   ```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 the Read operation that returns the dataset.

   ```C# DropDownTreeIndex.cshtml.cs
   public class DropDownTreeIndexModel : PageModel
   {
       public JsonResult OnGetDropDownTreeRead(int? id)
       { 
           var dropDownTreeData = result.Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
               .Select(item => new {
                   id = item.ID,
                   Name = item.Name,
                   hasChildren = item.HasChildren
               });

           return new JsonResult(dropDownTreeData);
       }

       public static IList<HierarchicalViewModel> result = new List<HierarchicalViewModel>()
       {
           new HierarchicalViewModel() { ID = 1, ParentID = null, HasChildren = true, Name = "Parent Item 1" },
           new HierarchicalViewModel() { ID = 2, ParentID = null, HasChildren = true, Name = "Parent Item 2" },
           new HierarchicalViewModel() { ID = 3, ParentID = null, HasChildren = true, Name = "Parent Item 3" },
           new HierarchicalViewModel() { ID = 4, ParentID = 1, HasChildren = false, Name = "Child Item 1" },
           new HierarchicalViewModel() { ID = 5, ParentID = 1, HasChildren = false, Name = "Child Item 2" },
           new HierarchicalViewModel() { ID = 6, ParentID = 2, HasChildren = false, Name = "Child Item 3" },
           new HierarchicalViewModel() { ID = 7, ParentID = 2, HasChildren = false, Name = "Child Item 4" },
           new HierarchicalViewModel() { ID = 8, ParentID = 3, HasChildren = false, Name = "Child Item 5" },
           new HierarchicalViewModel() { ID = 9, ParentID = 3, HasChildren = false, Name = "Child Item 6" }
       };
   }
   ```

   ```C# Model
   public class HierarchicalViewModel
   {
       public int ID { get; set; }
       public string Name { get; set; }
       public int? ParentID { get; set; }
       public bool HasChildren { get; set; }
   }
   ```

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

## Binding to a PageModel Property

To bind the DropDownTree to a property from the `PageModel`, follow the next steps:

1. Add a property to the `PageModel` that must bind to the DropDownTree.

   ```C# Index.cshtml.cs
       public class IndexModel : PageModel
       {
           [BindProperty]
           public string Employee { get; set; }

           public void OnGet()
           {
               Employee = "John"; 
           }
       }
   ```

2. Declare the `PageModel` at the top of the page.

   ```C#
       @page
       @model IndexModel
   ```

3. Bind the DropDownTree to the property using the `DropDownTreeFor()` configuration.

   ```Razor HtmlHelper
       @page
       @model IndexModel

       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()

       @(Html.Kendo().DropDownTreeFor(m => m.Employee))
   ```

   ```Razor TagHelper
       @page
       @model IndexModel

       @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
       @Html.AntiForgeryToken()
       @addTagHelper *, Kendo.Mvc

       <kendo-dropdowntree for="Employee">
       </kendo-datetimepicker>
   ```

## 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 DropDownTree](https://docs.telerik.com/kendo-ui/api/ui/dropdowntree)
* [Server-Side HtmlHelper API of the DropDownTree](https://www.telerik.com/aspnet-core-ui/documentation/api/dropdowntree)
* [Server-Side TagHelper API of the DropDownTree](https://www.telerik.com/aspnet-core-ui/documentation/api/taghelpers/dropdowntree)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
