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

[Solved] TreeView Children() Recursion?

5 Answers 237 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.
Sam Daniels
Top achievements
Rank 1
Sam Daniels asked on 12 May 2010, 02:19 PM
I'm trying to create a tree view bound to my model and display children recursively.  However, I can only get the children to display one level down.  Is there a built-in workaround?  This seems like basic functionality, but I've been unable to get >1 level down to display.  Am I missing something simple here?  Thanks.

Class:
public class person 
    public string name  { getset; } 
    public int age { getset; } 
    public List<person> children  { getset; } 
 

View:
<%= Html.Telerik().TreeView() 
        .Name("TreeView"
        .BindTo(Model, mappings => mappings.For<person>(binding => binding 
                                                                                .ItemDataBound((item, person) => 
                                                                                                   { 
                                                                                                       item.Text = person.Name; 
                                                                                                       item.Value = person.Age.ToString(); 
                                                                                                       }) 
                                                                                .Children(person => person.children))) 
%> 
 

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 12 May 2010, 02:23 PM
Hello Sam Daniels,

Did you check this and this forum threads? I think they will help you.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Sam Daniels
Top achievements
Rank 1
answered on 12 May 2010, 02:23 PM
Sorry, was looking only in the General Forum.  I now see a similar post about this same issue in the Tree View specific forum.
0
Sam Daniels
Top achievements
Rank 1
answered on 12 May 2010, 02:50 PM
I'm still having problems.  Do I need to modify NavigationItemContainerExtensions.cs as mentioned here?   If not, would someone please provide something specific for my model provided above?  No matter what I try (based on the examples and other posts in the forum) it's only displaying the first level of children.

Thanks in advance.
0
Atanas Korchev
Telerik team
answered on 12 May 2010, 02:57 PM
Hi Sam Daniels,

There are syntax errors in your ItemDataBound code which prevents the compiler from picking the right overload. The person's properties are name and age not Name and Age. Fixing your code will make it work.

Here is what I used to test this:

namespace MvcApplication7.Controllers
{
    public class person
    {
        public string name { get; set; }
        public int age { get; set; }
        public List<person> children { get; set; }
    }
 
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new[] { new person
                {
                    name = "Person 1",
                    children = new List<person>
                    {
                        new person
                        {
                            name = "child 1"
                        },
                        new person
                        {
                            name = "child 2"
                        }
                    }
                }
            });
        }
    }
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication7.Controllers.person>>" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title>Index</title>
    <%= Html.Telerik().StyleSheetRegistrar().DefaultGroup(group=> group
        .Add("telerik.common.css")
        .Add("telerik.vista.css"))
    %>
</head>
<body>
    <div>
        <%= Html.Telerik().TreeView()
        .Name("TreeView")
        .BindTo(Model, mappings => mappings.For<MvcApplication7.Controllers.person>(binding =>
            binding.ItemDataBound((item, person) =>
            {
                item.Text = person.name;
            })
            .Children(person => person.children)))
        %>
    </div>
    <%= Html.Telerik().ScriptRegistrar() %>
</body>
</html>

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Sam Daniels
Top achievements
Rank 1
answered on 12 May 2010, 03:52 PM
Thank you for your help.  I got it working.  The problem ended up being with the ScriptRegistrar, not with anything we were actually working on.  Because of this, I wasn't able to click on a node to expand it's children.  I was using ExpandAll(true) which was expanding the top level, but that was it.  With the scripts properly registered I can now see sub-children.

Oh, and the Typo that you mentioned wasn't the cause of the problem.  I had created the person class just to post on here as a simple example of my problem and had accidentally messed up the case.

Thanks again for your help.
Tags
TreeView
Asked by
Sam Daniels
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Sam Daniels
Top achievements
Rank 1
Share this question
or