Data binding in ASP.NET MVC Tree Grid Component

23 Aug 202310 minutes to read

The TreeGrid uses DataManager, which supports both RESTful JSON data services binding and local JavaScript object array binding. The DataSource property can be assigned either with the instance of DataManager or JavaScript object array collection.

It supports two kinds of data binding method:

  • Local data
  • Remote data

Binding with ajax

You can use TreeGrid DataSource property to bind the data source to TreeGrid from external Fetch request. In the below code we have fetched the data source from the server with the help of Fetch request and provided that to DataSource property by using onSuccess event of the Fetch.

@Html.EJS().Button("button").Content("Bind Data").Render()

@Html.EJS().TreeGrid("Ajax").Columns(col =>
{
   col.Field("TaskID").HeaderText("Task ID").Width(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
   col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(90).Add();
   col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

}).IdMapping("TaskID").ParentIdMapping("ParentItem").AllowPaging().PageSettings(p => p.PageSize(7)).TreeColumnIndex(1).Render()

<script>
    document.getElementById("button").addEventListener('click', () => {
        var treegrid = document.getElementById("Ajax").ej2_instances[0];
        treegrid.element.parentNode.insertBefore(button, treegrid.element);
        var fetch = new ej.base.Fetch("https://ej2services.syncfusion.com/production/web-services/api/SelfReferenceData", "GET");
        treegrid.showSpinner();
        fetch.send();
        fetch.onSuccess = function (data) {
            treegrid.hideSpinner();
            treegrid.dataSource = data;
        };
    });
</script>
public ActionResult AjaxBind()
{
  return View();
}

NOTE

If you bind the dataSource from this way, then it acts like a local dataSource. So you cannot perform any server side crud actions.

You can refer to our ASP.NET MVC Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET MVC Tree Grid example to knows how to present and manipulate data.

If you bind the dataSource from this way, then it acts like a local dataSource. So you cannot perform any server side crud actions.

Handling expandStateMapping

To denotes the expand status of parent row, define the ExpandStateMapping property of tree grid.

The ExpandStateMapping property maps the field name in data source, that denotes whether parent record is in expanded or collapsed state and this is useful to renders parent row in expanded or collapsed state based on this mapping property value in data source.

@Html.EJS().TreeGrid("TreeGrid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor")})
    .Columns(col =>
    {
     col.Field("TaskID").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
     col.Field("TaskName").HeaderText("Task Name").Width(190).Add();
     col.Field("Duration").HeaderText("Duration").Width(90).Add();
   }).Height(400).HasChildMapping("isParent").IdMapping("TaskID").ParentIdMapping("ParentValue")TreeColumnIndex(1).ExpandStateMapping("IsExpanded").Render()
public ActionResult ExpandStateMapping()
{
  return View();
}

public class TreeData
{
    public static List<TreeData> tree = new List<TreeData>();
    [System.ComponentModel.DataAnnotations.Key]
    public int TaskID { get; set; }
    public string TaskName { get; set; }

    public int Duration { get; set; }
    public int? ParentValue { get; set; }
    public bool? isParent { get; set; }

    public bool IsExpanded { get; set; }
    public TreeData() { }
    public static List<TreeData> GetTree()
    {
        if (tree.Count == 0)
        {
            int root = 0;
            for (var t = 1; t <= 500; t++)
            {
                Random ran = new Random();
                string math = (ran.Next() % 3) == 0 ? "High" : (ran.Next() % 2) == 0 ? "Release Breaker" : "Critical";
                string progr = (ran.Next() % 3) == 0 ? "Started" : (ran.Next() % 2) == 0 ? "Open" : "In Progress";
                root++;
                int rootItem = root;
                tree.Add(new TreeData() { TaskID = rootItem, TaskName = "Parent task " + rootItem.ToString(), isParent = true, IsExpanded = false, ParentValue = null, Duration = ran.Next(1, 50) });
                int parent = root;
                for (var d = 0; d < 1; d++)
                {
                    root++;
                    string value = ((parent + 1) % 3 == 0) ? "Low" : "Critical";
                    int par = parent + 1;
                    progr = (ran.Next() % 3) == 0 ? "In Progress" : (ran.Next() % 2) == 0 ? "Open" : "Validated";
                    int iD = root;
                    tree.Add(new TreeData() { TaskID = iD, TaskName = "Child task " + iD.ToString(), isParent = true, IsExpanded = false, ParentValue = rootItem, Duration = ran.Next(1, 50) });
                    int subparent = root;
                    for (var c = 0; c < 500; c++)
                    {
                        root++;
                        string val = ((subparent + c + 1) % 3 == 0) ? "Low" : "Critical";
                        int subchild = subparent + c + 1;
                        string progress = (ran.Next() % 3) == 0 ? "In Progress" : (ran.Next() % 2) == 0 ? "Open" : "Validated";
                        int childID = root ;
                        tree.Add(new TreeData() { TaskID = childID, TaskName = "sub Child task " + childID.ToString(), isParent = false, IsExpanded = false, ParentValue = subparent, Duration = ran.Next(1, 50) });
                    }
                }
            }
        }
        return tree;
    }
}

You can refer to our ASP.NET MVC Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET MVC Tree Grid example to knows how to present and manipulate data.