0

I am adding a Kendo TreeView to an ASP Net Core 3.1 Page. All the nodes are 'undefined'. From the picture it is trying to do the 'right' thing. Nesting is correct, expands and contracts correctly.

Undefined with Devtools

I shamelessly used code from here

My .cshtml

    @(
        Html.Kendo().TreeView()
        .Name("treeview")
        .DataTextField("Name")
        .TemplateId("treeview-template")
        .DataSource(dataSource1 => dataSource1
            .Custom()
            .Transport(t => t
                .Read(r => r.Url(Url.Page("Index", "Read_TreeViewData")))
            )
        )
    )

and .cs

        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 OnGetRead_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,
                    //expanded = item.Expanded,
                    //imageUrl = item.ImageUrl,
                    hasChildren = item.HasChildren
                });

            var jr = new JsonResult(result);
            return jr;
        }

I have tried several things, changing the DataTextField, calling .Add() in several places, trying to add a attribute. Does anyone know how to label the nodes with the Name from the HierarchicalViewModel?

3
  • You have TemplateId("treeview-template"). That overrides anything you have set as DataTextField. Do you have it defined on the page? Take that line out for starters and see what the difference is. Commented Aug 2, 2023 at 23:26
  • See this link as to how to configure a Kendo TreeView: stackoverflow.com/questions/17842222/… Commented Aug 2, 2023 at 23:35
  • Thanks @NigelK. Commenting out the treeview-template did the trick. If you submit an answer I'll accept it. Commented Aug 3, 2023 at 13:59

1 Answer 1

0

You have TemplateId("treeview-template") as part of the configuration. A template overrides anything set as DataTextField. The issue is is therefore with the template or the fact that it is missing from the page.

If you simply want the Name property rendered for each node then you can remove the template.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.