# ASP.NET Core TreeView Overview

The Telerik UI TreeView TagHelper and HtmlHelper for ASP.NET Core are server-side wrappers for the Kendo UI TreeView widget.

The TreeView displays hierarchical data in a traditional tree structure.

* [Demo page for the TreeView HtmlHelper](https://demos.telerik.com/aspnet-core/treeview/index)

* [Demo page for the TreeView TagHelper](https://demos.telerik.com/aspnet-core/treeview/tag-helper)

## Initializing the TreeView

The following example demonstrates how to define the TreeView.

```Razor HtmlHelper
    @(Html.Kendo().TreeView()
        .Name("treeview")
        .Items(items =>
        {
            items.Add().Text("Item 1").Expanded(true)
            .Items(subItems =>
            {
                subItems.Add().Text("Item 1.1");
                subItems.Add().Text("Item 1.2");
                subItems.Add().Text("Item 1.3");
            });

            items.Add().Text("Item 2")
            .Items(subItems =>
            {
                subItems.Add().Text("Item 2.1");
                subItems.Add().Text("Item 2.2");
                subItems.Add().Text("Item 2.3");
            });

            items.Add().Text("Item 3");
        })
    )
```

```Razor TagHelper
    @addTagHelper *, Kendo.Mvc

	<kendo-treeview name="treeview">
		<items>
			<treeview-item expanded="true" text="Item 1">
				<items>
                    <treeview-item text="Item 1.1"></treeview-item>
                    <treeview-item text="Item 1.2"></treeview-item>
                    <treeview-item text="Item 1.3"></treeview-item>
                </items>
			</treeview-item>
			<treeview-item text="Item 2">
				<items>
                    <treeview-item text="Item 2.1"></treeview-item>
                    <treeview-item text="Item 2.2"></treeview-item>
                    <treeview-item text="Item 2.3"></treeview-item>
                </items>
			</treeview-item>
			<treeview-item text="Item 3"></treeview-item>
		</items>
	</kendo-treeview>
```

```C# Controller
public static IList<HierarchicalViewModel> GetHierarchicalData()
{
    var result = new List<HierarchicalViewModel>()
    {
        new HierarchicalViewModel() { ID = 1, ParentID = null, HasChildren = true, Name = "Parent item" },
        new HierarchicalViewModel() { ID = 2, ParentID = 1, HasChildren = true, Name = "Parent item" },
        new HierarchicalViewModel() { ID = 3, ParentID = 1, HasChildren = false, Name = "Item" },
        new HierarchicalViewModel() { ID = 4, ParentID = 2, HasChildren = false, Name = "Item" },
        new HierarchicalViewModel() { ID = 5, ParentID = 2, HasChildren = false, Name = "Item" }
    };

    return result;
}

public IActionResult Read_TreeViewData(int? id)
{
    var result = GetHierarchicalData()
        .Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
        .Select(item => new {
            id = item.ID,
            Name = item.Name,
            hasChildren = item.HasChildren
        });

    return Json(result);
}
```

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

> Do not use the names of the [`kendo.data.Node` fields and methods](https://docs.telerik.com/kendo-ui/api/data/node) (for example, `children`) as fields in the TreeView data.

## Basic Configuration

The following example demonstrates how to configure the TreeView to bind to remote data and access the TreeView instance.

```Razor HtmlHelper
    @(Html.Kendo().TreeView()
        .Name("treeview")
        .DragAndDrop(true)
        .DataTextField("Name")
        .DataSource(dataSource => dataSource
            .Read(read => read
                .Action("ReadItems", "TreeView")
            )
        )
    )

    <script type="text/javascript">
        $(function () {
            // Select the TreeView element by using its "Name()" to get the client-side instance.
            var treeview = $("#treeview").data("kendoTreeView");
            console.log(treeview);
        });
    </script>
```

```Razor TagHelper
    <kendo-treeview name="treeview" drag-and-drop="true" datatextfield="Name">
        <hierarchical-datasource>
            <transport>
                <read url="@Url.Action("ReadItems", "Treeview")"/>
            </transport>
            <schema>
                <hierarchical-model id="id"></hierarchical-model>
            </schema>
        </hierarchical-datasource>
    </kendo-treeview>

    <script type="text/javascript">
        $(function () {
            // Select the TreeView element by using its "Name()" to get the client-side instance.
            var treeview = $("#treeview").data("kendoTreeView");
            console.log(treeview);
        });
    </script>
```

```C# Controller
    public List<HierarchicalViewModel> GetHierarchicalData()
    {
        var data = new List<HierarchicalViewModel>()
        {
            new HierarchicalViewModel() { ID = 1, ParentID = null, HasChildren = true, Name = "Parent item" },
            new HierarchicalViewModel() { ID = 2, ParentID = 1, HasChildren = true, Name = "Item 1" },
            new HierarchicalViewModel() { ID = 3, ParentID = 1, HasChildren = false, Name = "Item 2" },
            new HierarchicalViewModel() { ID = 4, ParentID = 2, HasChildren = false, Name = "Item 1.1" },
            new HierarchicalViewModel() { ID = 5, ParentID = 2, HasChildren = false, Name = "Item 1.2" }
        };
        return data;
    }

    public JsonResult ReadItems(int? id)
    {
        var result = GetHierarchicalData()
        .Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
        .Select(item => new {
            id = item.ID,
            Name = item.Name,
            hasChildren = item.HasChildren
        });

        return Json(result);
    }
```

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

> Do not use the names of the [`kendo.data.Node` fields and methods](https://docs.telerik.com/kendo-ui/api/data/node) (for example, `children`) as fields in the TreeView data.

## Functionality and Features

| Feature                                                                                                                       | Description                                                                                                      |
| ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [Data Binding](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/binding)                 | Explore the available data-binding approaches of the TreeView.                                                   |
| [Animations](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/animations)                | Configure different animation effects when expanding or collapsing the TreeView nodes.                           |
| [Appearance](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/appearance)                | The TreeView has built-in styling options that allow you to customize its appearance.                            |
| [Items](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/items)                          | Set up the TreeView by adding items within the helper declaration.                                               |
| [Images](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/images)                        | Enhance the TreeView items by adding images or sprites.                                                          |
| [Drag and Drop](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/drag-drop)              | Enable the drag-and-drop feature of the component.                                                               |
| [Checkboxes](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/checkboxes)                | Render the TreeView items with checkboxes.                                                                       |
| [Templates](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/templates)                  | To take full control over the rendering of the TreeView items, use templates.                                    |
| [Events](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/events)                        | The component exposes various events that allow you to respond to user actions.                                  |
| [Accessibility](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/accessibility/overview) | The TreeView is accessible by screen readers and provides WAI-ARIA, Section 508, WCAG 2.2, and keyboard support. |

## Next Steps

* [Getting Started with the TreeView](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/treeview/getting-started)

* [Basic Usage of the TreeView HtmlHelper for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/treeview/basic-usage)

* [Basic Usage of the TreeView TagHelper for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/treeview/tag-helper)

* [TreeView in Razor Pages](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/data-management/pager/razor-page)

## See Also

* [Using the API of the TreeView for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/treeview/api)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
