New to Telerik UI for ASP.NET Core? Download free 30-day trial

DropDownTree in Razor Pages

This article describes how to configure a Remote DataSource of a Telerik DropDownTree in Razor Pages scenario.

In order to set up the ComboBox component bindings, you need to configure the Read method of its DataSource instance. The URL in this method should refer the name of the method in the PageModel. In this method, you can also pass additional parameters, such as filter string and antiforgery token (see dataFunction). See the implementation details in the example below, and for the full project with RazorPages examples, visit our GitHub repository.

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

    @(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")))
            )

    )

    <script>
        function forgeryToken() {
            return kendo.antiForgeryTokens();
        }
    </script>
    @inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
    @Html.AntiForgeryToken()

    <kendo-dropdowntree datatextfield="Name" datavaluefield="id" name="dropdowntree" auto-width="true" style="width: 100%" auto-close="false">
        <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>

    <script>
        function forgeryToken() {
            return kendo.antiForgeryTokens();
        }
    </script>

        public JsonResult OnGetDropDownTreeRead(int? id)
        { 
            var data = dbContext.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