Getting Started with ASP.NET Core TreeView Control

23 Jul 20265 minutes to read

This section briefly explains how to include the ASP.NET Core TreeView control in your ASP.NET Core Web App using Visual Studio.

Create ASP.NET Core Web App with Razor Pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the ASP.NET Core Extension.

Install the required ASP.NET Core packages

To add ASP.NET Core TreeView control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), then search and install Syncfusion.AspNetCore.Navigations and Syncfusion.AspNetCore.Themes. All Syncfusion ASP.NET Core packages are available in nuget.org. See the NuGet packages topic for more details.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.AspNetCore.Navigations -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core tag helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Navigations and Syncfusion.AspNetCore.Base tag helpers.

@addTagHelper *, Syncfusion.AspNetCore.Navigations
@addTagHelper *, Syncfusion.AspNetCore.Base

Add stylesheet and script resources

The theme stylesheet and script can be referenced from NuGet through Static Web Assets. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    ...
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    <script src="_content/Syncfusion.AspNetCore.Navigations/scripts/sf-treeview.min.js"></script>
</head>

Register the script manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager (<ejs-scripts>) at the end of the <body> element as shown below.

<body>
    ...
    <!-- Syncfusion ASP.NET Core Script Manager -->
    <ejs-scripts></ejs-scripts>
</body>

Add the ASP.NET Core TreeView control

Add the ASP.NET Core TreeView control in the ~/Pages/Index.cshtml file.

<ejs-treeview id="listdata"></ejs-treeview>

Binding data source

The TreeView control can load data from either local data sources or remote data services. This is achieved through the dataSource property available within the fields configuration. The dataSource property accepts both an array of JavaScript objects and a DataManager instance.

In the following example, the TreeView is bound to a local JSON data collection that represents countries and their corresponding states.

@{
    List<object> listdata = new List<object>
    {
        new { id = 1, name = "Australia", hasChild = true, expanded = true },
        new { id = 2, pid = 1, name = "New South Wales" },
        new { id = 3, pid = 1, name = "Victoria" },
        new { id = 4, pid = 1, name = "South Australia" },
        new { id = 6, pid = 1, name = "Western Australia" },
        new { id = 7, name = "Brazil", hasChild = true },
        new { id = 8, pid = 7, name = "Paraná" },
        new { id = 9, pid = 7, name = "Ceará" },
        new { id = 10, pid = 7, name = "Acre" },
        new { id = 11, name = "China", hasChild = true },
        new { id = 12, pid = 11, name = "Guangzhou" },
        new { id = 13, pid = 11, name = "Shanghai" },
        new { id = 14, pid = 11, name = "Beijing" },
        new { id = 15, pid = 11, name = "Shantou" },
        new { id = 16, name = "France", hasChild = true },
        new { id = 17, pid = 16, name = "Pays de la Loire" },
        new { id = 18, pid = 16, name = "Aquitaine" },
        new { id = 19, pid = 16, name = "Brittany" },
        new { id = 20, pid = 16, name = "Lorraine" },
        new { id = 21, name = "India", hasChild = true },
        new { id = 22, pid = 21, name = "Assam" },
        new { id = 23, pid = 21, name = "Bihar" },
        new { id = 24, pid = 21, name = "Tamil Nadu" }
    };
}
<ejs-treeview id="listdata">
    <e-treeview-fields  dataSource="listdata" id="id" parentId="pid" text="name" hasChildren="hasChild" expanded="expanded"></e-treeview-fields>
</ejs-treeview>

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core TreeView control will render in your default web browser.

ASP.NET Core TreeView Control

NOTE

View Sample in GitHub.

See also

  1. Getting Started with ASP.NET Core in Visual Studio Mac
  2. Getting Started with ASP.NET Core MVC using Tag Helper