将Json数据绑定到Kendo treeview MVC4

时间:2014-09-25 05:47:48

标签: json asp.net-mvc-4 kendo-treeview

我的剑道树视图有许多子节点和子节点。使用冗余代码获取子子节点的问题

这是我的代码。

 nodes = rep.GetTreeViewData(CompanyId);

        var productTreeData = nodes.Select(p => new
        {
            Text = p.CategoryName,
            Url = p.ActionLink,
            hasChildren = p.SubCategories.Any(),
            // Expanded = true,
            EntityType = p.EntityType,
            Selected = currurl.ToLower() == p.ActionLink.ToLower() ? true : false,
            ImageUrl = p.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif",

            Children = p.SubCategories.Select(c => new
            {
                Text = c.SubCategoryName,
                Url = c.ActionLink,
                // Expanded = true,
                hasChildren = c.SubCategories.Any(),
                EntityType = c.EntityType,
                Selected = currurl.ToLower() == c.ActionLink.ToLower() ? true : false,
                ImageUrl = c.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif",

                Children = c.SubCategories.Select(d => new
                {
                    Text = d.SubCategoryName,
                    Url = d.ActionLink,
                    // Expanded = true,
                    hasChildren = d.SubCategories.Any(),
                    EntityType = d.EntityType,
                    Selected = currurl.ToLower() == d.ActionLink.ToLower() ? true : false,
                    ImageUrl = d.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif",


                    Children = d.SubCategories.Select(f => new
                    {
                        Text = f.SubCategoryName,
                        Url = f.ActionLink,
                        //Expanded = true,
                        hasChildren = f.SubCategories.Any(),
                        EntityType = f.EntityType,
                        Selected = currurl.ToLower() == f.ActionLink.ToLower() ? true : false,
                        ImageUrl = f.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif",

                        Children = f.SubCategories.Select(g => new
                        {
                            Text = g.SubCategoryName,
                            Url = g.ActionLink,
                            // Expanded = true,
                            hasChildren = g.SubCategories.Any(),
                            EntityType = g.EntityType,
                            Selected = currurl.ToLower() == g.ActionLink.ToLower() ? true : false,
                            ImageUrl = g.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif",

                        })

                    })

                })

            })

        });

这是我的观点

 @(Html.Kendo().TreeView()
                                      .Name("treeview-right")
                                      .ExpandAll(true)
                                      //.LoadOnDemand(false)
                                      //.Events(events=>events.DataBound("DataBound"))
                                      .DataSource(d => d
                                      .Model(m => m
                                      .HasChildren("hasChildren")
                                      .Children("Children"))
                                      .Read(r => r.Action("_ProductTree", "Home")))
                                      .DataTextField("Text")
                                      .DataUrlField("Url")
                                      .DataImageUrlField("ImageUrl")
                                      )

通过这个过程,我可以将个体父母的所有孩子和子孩子提升到四级等级。如果我需要另一个等级,我需要再编写一个子代码。

我在2天内挣扎着简化这个过程。

由于

1 个答案:

答案 0 :(得分:0)

这看起来可以通过递归来解决。这应该非常接近。

 var productTreeData = new ListofYourTreeViewModelHere(); 

 foreach (var node in rep.GetTreeViewData(CompanyId))
 {
    productTreeData.Add(FillTree(node));
 }
...
}


private YourTreeViewModelHere FillTree(p)
{
    var tv = new YourTreeViewModelHere();

    tv.Text = p.CategoryName,
    tv.Url = p.ActionLink,
    tv.hasChildren = p.SubCategories.Any(),
    tv.EntityType = p.EntityType,
    tv. Selected = currurl.ToLower() == p.ActionLink.ToLower() ? true : false,
    tv.ImageUrl = p.EntityType == "Company" ? "/Content/Images/company.gif" : "/Content/Images/geolocation1.gif";

     foreach(var sub in p.SubCategories)
     {
         tv.Children.Add(FillTree(x));
     }
  }