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

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

This article describes how to configure the OrgChart component in a Razor Pages scenario.

Getting Started

To connect the OrgChart to a data set retrieved from a remote endpoint in a Razor Pages application, proceed with the following steps:

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

    HtmlHelper_Index.cshtml
        @page
        @model IndexModel
    
        @(Html.Kendo().OrgChart<OrgChartEmployeeViewModel>()
           .Name("orgchart")
           .DataSource(dataSource => dataSource
               .Read(r => r.Url("/Index?handler=Read").Data("forgeryToken"))
               .Model(m => {
                   m.Id(f => f.ID);
                   m.ParentId(f => f.ParentID);
                   m.Name(f => f.Name);
                   m.Title(f => f.Title);
                   m.Avatar(f => f.Avatar);
                   m.Expanded(f=>f.Expanded);
               })
           )
        
  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.

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

    Additional parameters can also be supplied.

    Razor
        <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.

    Index.cshtml.cs
        public static IList<OrgChartEmployeeViewModel> employees;
    
        public void OnGet(string culture)
        {
            if (!String.IsNullOrEmpty(culture))
            {
                CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(culture);
            }
    
            if (employees == null)
            {
                employees = GetData();
            }
        }
    
        public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request, int? id)
        {
            return new JsonResult(employees.ToDataSourceResult(request));
        }
    
        private List<OrgChartEmployeeViewModel> GetData()
        {
            var source = new List<OrgChartEmployeeViewModel>();
            var path = "https://demos.telerik.com/aspnet-core/shared/web/treelist/people/";
    
            source.Add(new OrgChartEmployeeViewModel() { ID = 1, Name = "Gevin Bell", Title = "CEO", Expanded = true, Avatar = path +"1.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 2, Name = "Clevey Thrustfield", Title = "COO", Expanded = true, ParentID = 1, Avatar = path +"2.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 3, Name = "Carol Baker", Title = "CFO", Expanded = false, ParentID = 1, Avatar = path +"3.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 4, Name = "Kendra Howell", Title = "CMO", Expanded = false, ParentID = 1, Avatar = path +"4.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 5, Name = "Sean Rusell", Title = "Financial Manager", Expanded = true, ParentID = 3, Avatar = path +"5.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 6, Name = "Steven North", Title = "Senior Manager", Expanded = false, ParentID = 3, Avatar = path +"6.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 7, Name = "Michelle Hudson", Title = "Operations Manager", Expanded = true, ParentID = 2, Avatar = path +"7.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 8, Name = "Andrew Berry", Title = "Team Lead", ParentID = 5, Avatar = path +"8.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 9, Name = "Jake Miller", Title = "Junior Accountant", ParentID = 5, Avatar = path +"9.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 10, Name = "Austin Piper", Title = "Accountant", ParentID = 5, Avatar = path +"10.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 11, Name = "Dilyana Newman", Title = "Accountant", ParentID = 5, Avatar = path +"11.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 12, Name = "Eva Andrews", Title = "Team Lead", ParentID = 6, Avatar = path +"12.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 13, Name = "Kaya Nilsen", Title = "Financial Specialist", ParentID = 6, Avatar = path +"13.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 14, Name = "Elena Austin", Title = "Team Lead", ParentID = 4, Avatar = path +"14.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 15, Name = "Lora Samuels", Title = "Lawyer", ParentID = 4, Avatar = path +"15.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 16, Name = "Lillian Carr", Title = "Operator", ParentID = 7, Avatar = path +"17.jpg" });
            source.Add(new OrgChartEmployeeViewModel() { ID = 17, Name = "David Henderson", Title = "Team Lead", ParentID = 7, Avatar = path +"16.jpg" });
    
            return source;
        }

See Also

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