This is a migrated thread and some comments may be shown as answers.

Binding entity framework to TreeView MVC

4 Answers 331 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Victor
Top achievements
Rank 1
Victor asked on 20 Dec 2017, 05:24 AM

Hi everyone,

I am developing an ASP.NET Web Application, in which I am accessing an SQL database to retrieve information from a table.

The information accessed from SQL (one single table) is stored in an entity framework. Each item in the framework contains ID, ParentID and name.

In my application so far, I can print every item in the entity framework and its information (using a for loop).

 

I would like to generate a TreeView using Telerik to display each item, and its respective children items (we can figure that out from ParentID). I had a look at demos but none seemed to apply to my case, as the data that is contained in the entity framework is dynamic (it will not always be the same, sometimes they will be more or less levels in the tree,etc), so we cannot hard code the tree elements.

 

Does anyone know if this can be done?

Thanks.

4 Answers, 1 is accepted

Sort by
0
Victor
Top achievements
Rank 1
answered on 20 Dec 2017, 05:42 AM
Just to clarify, I am coding using MVC and that is my only option.
0
Bozhidar
Telerik team
answered on 21 Dec 2017, 11:14 AM
Hi,

Please take a look at the following demo, which shows how you can bind the Kendo MVC TreeView to a self-referencing table through EntityFramework:
https://demos.telerik.com/aspnet-mvc/treeview/remote-data-binding

If you need any further assistance, please don't hesitate to contact us again.

Regards,
Bozhidar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Steve
Top achievements
Rank 1
answered on 22 Mar 2018, 05:17 PM

I am trying to get a treeview to show in a popup window from a custom command button in a grid.

Following the remote binding demo, I get an error:

Uncaught TypeError: item.level is not a function
    at eval (eval at compile (kendo.all.min.js:25), <anonymous>:3:222)

From controller:

        public JsonResult Move_Read(long? id)
        {
            using (var context = new Entities(DSRConfigurationManager.GetInstance().GetDatabaseConnectionString()))
            {
                var containers = from c in context.cdsl_container
                                 where (c.cdsl_parent_container_id ?? 0) == (id ?? 0)
                                 select new
                                 {
                                     id = c.cdsl_container_id,
                                     Name = c.container_label,
                                     hasChildren = c.cdsl_container1.Any(),
                                     ParentID = c.cdsl_parent_container_id
                                 };
                return Json(containers, JsonRequestBehavior.AllowGet);
            }
        }

From cshtml:

<script type="text/x-kendo-template" id="MoveTemplate">
    @(Html.Kendo().TreeView()
        .Name("treeview")
        .DataTextField("Name")
        .DataSource(dataSource => dataSource
                        .Read(read => read.Action("Move_Read", "Container"))
                    )
        .ToClientTemplate()
    )
</script>

0
Steve
Top achievements
Rank 1
answered on 23 Mar 2018, 01:12 AM
Followup: The root cause of the problem had to do with an "ObjectContext has been disposed" error on the database reader.  All is working now.
Tags
TreeView
Asked by
Victor
Top achievements
Rank 1
Answers by
Victor
Top achievements
Rank 1
Bozhidar
Telerik team
Steve
Top achievements
Rank 1
Share this question
or