New to Telerik UI for ASP.NET CoreStart a free 30-day trial

TreeView 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 TreeView for ASP.NET Core in Razor Pages applications.

This article describes how to configure the TreeView component in a Razor Pages scenario. For the complete project, refer to the TreeView in Razor Pages example.

Getting Started

The following example demonstrates how to configure the TreeView in a Razor Pages scenario and bind it to a remote data collection.

  1. Define the TreeView and specify the Read request URL in the DataSource configuration. The URL must refer to the method name in the PageModel.

    Razor
        @page
        @model IndexModel
        @using Kendo.Mvc.UI
    
        @(Html.Kendo().TreeView()
           .Name("treeview")
           .DataTextField("Name")
           .DataSource(dataSource => dataSource
               .Custom()
               .Transport(t => t
                   .Read(r => r.Url(Url.Page("Index", "TreeViewRead")).Data("forgeryToken").Type(HttpVerbs.Post)))
               )
       )
  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 data operation.

    C#
        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" }
        };
    
        public void OnGet(string culture)
        {
            if (!String.IsNullOrEmpty(culture))
            {
                CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(culture);
            }
        }
    
        public JsonResult OnPostTreeViewRead(int? id)
        {
            var data = 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(data);
        }

See Also

In this article
Getting StartedSee Also
Not finding the help you need?
Contact Support