# Loading and expanding only the first level data in the TreeList

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC TreeList</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.3.1010 version</td>
 </tr>
</table>

## Description

How do I expand only the first item on page load in the Telerik UI for ASP.NET MVC TreeList.

## Solution

To achieve the desired results:

1. Set the TreeList [`AutoBind()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/treelistbuilder#autobindsystemboolean) property explicitly to `false`.
2. Make a read request (the custom call which will return the initial data) by using the client-side dataSource's [`read()`](https://docs.telerik.com/kendo-ui/api/data/datasource/methods/read) method.
3. Use the client-side TreeListDataSource's [`load()`](https://docs.telerik.com/kendo-ui/api/data/treelistdatasource/methods/load) method to load the root.
4. To expand only the items which are in the view, use the client-side [`expand()`](https://docs.telerik.com/kendo-ui/api/ui/treelist/methods/expand) method - this will not make a request for non-loaded items.

```Razor Index.cshtml
    @(Html.Kendo().TreeList<EmployeeDirectoryRemoteModel>()
        .Name("treelist")
        .Columns(columns =>
        {
            columns.Add().Field(f => f.FirstName).Width(250);
            columns.Add().Field(e => e.LastName);
            columns.Add().Field(e => e.Position);
            columns.Add().Field(e => e.Extension).Title("Ext").Format("{0:#}");
        })
        .AutoBind(false)
        .DataSource(dataSource => dataSource
            .Read(read => read.Action("Index", "EmployeeDirectory"))
            .Model(m => {
                m.Id(f => f.EmployeeId);
                m.ParentId(f => f.ReportsTo);
                m.Field(f => f.FirstName);
                m.Field(f => f.LastName);
                m.Field(f => f.ReportsTo);
            })
        )
    )
```

```JS script.js
    <script>
        $(document).ready(function () {

            setTimeout(function () {
                var treelist = $("#treelist").data("kendoTreeList");

                treelist.dataSource.read().then(function () {
                    var root = treelist.dataSource.at(0); // Gather the root return treelist.dataSource.load(root); // Load the root item.
    
                }).then(function () {
                    var root = treelist.dataSource.at(0); // Obtain the root item.
                    var children = treelist.dataSource.childNodes(root); // Get a reference of the child nodes.
                    treelist.expand(treelist.items());  // Expand the items that are only within the boundaries of the view.
                });
            })
        })
    </script>
```

```C# Controller.cs
    public class EmployeeDirectoryController : Controller
    {
        private EmployeeDirectoryService employeeDirectory;

        public EmployeeDirectoryController()
        {
            employeeDirectory = new EmployeeDirectoryService(new DemoDBContext());
        }

        public JsonResult Index([DataSourceRequest] DataSourceRequest request, int? id)
        {
            var result = ((EmployeeDirectoryService) employeeDirectory).GetAllRemote().ToTreeDataSourceResult(request,
                e => e.EmployeeId,
                e => e.ReportsTo,
                e => id.HasValue ? e.ReportsTo == id : e.ReportsTo == null,
                e => e
            );

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
```

## More ASP.NET MVC TreeList Resources

* [ASP.NET MVC TreeList Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/treelist/overview)

* [ASP.NET MVC TreeList Demos](https://demos.telerik.com/aspnet-mvc/treelist/index)

* [ASP.NET MVC TreeList Product Page](https://www.telerik.com/aspnet-mvc/treelist)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the TreeList for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/treelist)
* [Server-Side API Reference of the TreeList for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/treelist)
* [Server-Side TagHelper API Reference of the TreeList for ASP.NET MVC](https://docs.telerik.com/aspnet-core/api/taghelpers/treelist)
* [Telerik REPL: Load and expand only the first level data in the TreeList](https://netcorerepl.telerik.com/wxFEQyOL10jhfbhi36)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
