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

[Solved] treeview structure dynamically from database

8 Answers 141 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
saroj
Top achievements
Rank 1
saroj asked on 26 Jul 2012, 06:01 AM
Hi Support,


I need to create a treeview structure dynamically
 
Let me describe my scenario: I have four tables - Country, City, State and Suburb.
City has Country_ID as foreign key.
State has City_ID as foreign key.
Suburb has State_ID as foreign key.


 When I pass the Country_ID I need to show all the Cities related under that Country, similarly state related under that city and suburb related under that state in a tree structure.


How can I create the model for this scenario and does Telerikcontrol support this scenario.


Thanks
Saroj

8 Answers, 1 is accepted

Sort by
0
Alex Gyoshev
Telerik team
answered on 26 Jul 2012, 06:27 AM
Hello Saroj,

Please refer to the treeview binding to model example. It shows a scenario that is similar to yours.

Regards,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
saroj
Top achievements
Rank 1
answered on 26 Jul 2012, 06:40 AM
Thanks Alex for prompt reply,

What will be the structure of the Model in that example.

Also can you please explain what this view is returning in that example.

public ActionResult DataBindingToModel()
        {
            return View(GetCategories());
        }


Thanks in advance
Saroj
0
Alex Gyoshev
Telerik team
answered on 26 Jul 2012, 06:52 AM
You can open the examples project from the extensions' installation and browse through the code, from the database, through the model, to the controller and view.

All the best,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
saroj
Top achievements
Rank 1
answered on 26 Jul 2012, 07:01 AM
Hi alex,

Where can i find  "examples project from the extensions' installation", Could you please provide me the link to browse them.

Thanks
Saroj
0
Alex Gyoshev
Telerik team
answered on 26 Jul 2012, 07:22 AM
The default installation path is C:\Program Files (x86)\Telerik\Extensions for ASP.NET MVC Q2 2012\Examples\Telerik.Web.Mvc.Examples.

Regards,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
saroj
Top achievements
Rank 1
answered on 30 Jul 2012, 07:02 AM
Hi Alex,

Below is my code, still i am having problem, getting error "Sequence contain no elements", can't display treeview structure. Any idea, please.

Controller
public ActionResult DataBindingToModel()
        {
            ReferencesFiles _ReferencesFiles = new ReferencesFiles();
            var _department = new ReferencesFiles().DepartmentTreeview().AsEnumerable();
            return PartialView("_TreeviewDisplay", _department.ToList());
        }

Class
 public IEnumerable<SYSDepartmentTreeviewModel> DepartmentTreeview()
        {
            using (var context = new AUSFLEETEntities())
            {
                 var _depart = from p in context.SYS_DEPARTMENT
                                  
                                  select new SYSDepartmentTreeviewModel
                                   {
                                       DEPARTMENT_ID = p.DEPARTMENT_ID,
                                       DEPARTMENT_DESCRIPTION = p.DEPARTMENT_DESCRIPTION
                                       Branchs = (from a in context.SYS_BRANCH
                                                  where a.DEPARTMENT_ID == p.DEPARTMENT_ID
                                                  select new SYSBranchTreeviewModel
                                                    {
                                                        DEPARTMENT_ID = p.DEPARTMENT_ID,
                                                        BRANCH_ID=a.BRANCH_ID,
                                                        BRANCH_DESCRIPTION = a.BRANCH_DESCRIPTION,                                                                   
                                                            
                                        Regions = (from c in context.SYS_REGION
                                                   where a.BRANCH_ID == c.BRANCH_ID
                                                   select new SYSRegionTreeviewModel
                                                    {
                                                    BRANCH_ID = c.BRANCH_ID,
                                                    REGION_ID= c.REGION_ID,
                                                    REGION_DESCRIPTION = c.REGION_DESCRIPTION
                                                    })
                                                                                                                                 
                                                        })
                                   };
   return _depart.ToList();
            }
        }

Model

 [KnownType(typeof(SYSDepartmentTreeviewModel))]
    public class SYSDepartmentTreeviewModel
    {
        public int DEPARTMENT_ID { get; set; }
        public string DEPARTMENT_DESCRIPTION { get; set; }
        public IEnumerable <SYSBranchTreeviewModel> Branchs { get; set; }
    }
    [KnownType(typeof(SYSBranchTreeviewModel))]
    public class SYSBranchTreeviewModel
    {
        public int BRANCH_ID { get; set; }
        public string BRANCH_DESCRIPTION { get; set; }
        public int DEPARTMENT_ID { get; set; }
        public IEnumerable<SYSDepartmentTreeviewModel> Departments { get; set; }
        public IEnumerable<SYSRegionTreeviewModel> Regions { get; set; }
    }
    [KnownType(typeof(SYSRegionTreeviewModel))]
    public class SYSRegionTreeviewModel
    {
       public int REGION_ID { get; set; }
        public string REGION_DESCRIPTION { get; set; }
        public int BRANCH_ID { get; set; }
        public IEnumerable<SYSBranchTreeviewModel> Branchs { get; set; }
    }

PartialView

@model IEnumerable<SYSDepartmentTreeviewModel> 
@(Html.Telerik().TreeView()
        .Name("TreeView")
        .BindTo(Model, mappings =>
        {
            mappings.For<SYSDepartmentTreeviewModel>(binding => binding
                    .ItemDataBound((item, department) =>
                    {
                        item.Text = department.DEPARTMENT_DESCRIPTION;
                    })
               .Children(department => department.Branchs)
            );
 mappings.For<SYSBranchTreeviewModel>(binding => binding
                    .ItemDataBound((item, branch) =>
                    {
                        item.Text = branch.BRANCH_DESCRIPTION;
                    })
                .Children(branch => branch.Regions)
                    );
        })
)


0
Alex Gyoshev
Telerik team
answered on 30 Jul 2012, 09:29 AM
Hello Saroj,

We cannot determine the problem from a code listing. If you need help with your project, please open a support ticket and attach a sample that reproduces the problem.

Kind regards,
Alex Gyoshev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
saroj
Top achievements
Rank 1
answered on 02 Aug 2012, 12:05 AM
thanks for help, i have resovled the issue
Tags
TreeView
Asked by
saroj
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
saroj
Top achievements
Rank 1
Share this question
or