Binding TreeView to DataSource

2 Answers 105 Views
TreeView
Alexandre
Top achievements
Rank 2
Iron
Iron
Iron
Alexandre asked on 09 May 2022, 10:15 AM

Hello,

 

I am trying to bind my treeview to a datasource provided by a controller function, using two examples provided by Telerik, on the web site and at the demos project. My goal is to use the script call from used in one demo with the controller data source used in the other, but I am not being able to do it.

This is the script code:

 <script>
        var serviceRoot = "https://demos.telerik.com/kendo-ui/service";
        homogeneous = new kendo.data.HierarchicalDataSource({
            transport: {
                read: {
                    url: "api/Servicos/Employees",
                    dataType: "jsonp"
                }
            },
            schema: {
                model: {
                    id: "SetorId",
                    hasChildren: "hasChildren"
                }
            }
        });

        $("#treeview").kendoTreeView({
            dataSource: homogeneous,
            dataTextField: "Nome"
        });
    </script>

And this is the Controller code:

        [Route("Employees")]
        [HttpGet]
        public JsonResult Employees()
        {
            var result = _unitOfWork.SetorSolicitante.GetAll();
            {
                var employees = from e in result
                                select new
                                {
                                    id = e.SetorSolicitanteId,
                                    Name = e.Nome,
                                    hasChildren = (from q in _unitOfWork.SetorSolicitante.GetAll()
                                                   where (q.SetorPaiId == e.SetorSolicitanteId)
                                                   select q
                                                   ).Count() > 0
                                };

                return new JsonResult(employees.ToList());
            }
        }

My table references itself, where SetorPaiId indicates if the SetorId is a child of it.

Could I get some assistance on that issue?

Regards,

Alexandre

   

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 12 May 2022, 08:02 AM

Hello, Alexandre,

Thank you for the provided code.

Could you please set dataType to json instead of jsonp when running through localhost?

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Alexandre
Top achievements
Rank 2
Iron
Iron
Iron
answered on 12 May 2022, 09:58 AM

Hello Martin,

Thank you for your assistance, but it still did not work when changing dataType. Would you be able to make a single razor page project with just this control so that I could how it works?

Regards,

Alexandre

Martin
Telerik team
commented on 17 May 2022, 07:08 AM

Attached you will find a small .Net Core project. Let me know if it would be helpful.
Tags
TreeView
Asked by
Alexandre
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Martin
Telerik team
Alexandre
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or