在Treeview中加载类别和项目

时间:2014-03-19 04:27:10

标签: asp.net-mvc asp.net-mvc-4 razor kendo-ui

您好我正在尝试使用Categories然后产品加载树视图。我正在使用MVC 4 Razor。

我终于能够在单独的树视图中加载数据,看看我是否至少可以得到它。

但这就是我现在的位置。

@(Html.Kendo().TreeView()
    .Name("treeview")
    .HtmlAttributes(new {@class="tree-section" })
    .DataTextField("Name")
    .DataSource(dataSource => dataSource
        .Read(read => read
            .Action("treeItems", "Home")
        )
    )
)
<script>
    $(document).ready(function() {
        var treeview = ("#treeview").data("kendoTreeView");
    });

</script>

这是我的控制器

public JsonResult treeItems(int? id)
{
    var dataContext = new myContext();

    //var cat = from e in dataContext.Categories
    //    where (id.HasValue ? e.ParentId == id : e.ParentId == null)
    //    select new
    //    {
    //        id = e.CategoryId,
    //        Name = e.Name,
    //        hasChildren = e.Categories1.Any()
    //    };

    var prods = from c in dataContext.Items
                join a in dataContext.SubItems on c.ItemId equals a.ItemId
                where (id.HasValue ? a.ParentItemId == id : a.ParentItemId == null)
                select new
                {
                    id = c.ItemId,
                    Name = c.Name,
                    hasChildren = c.SubItems.Any()
                };
    return Json(prods, JsonRequestBehavior.AllowGet);
}

如果我将它们分别加载到2个不同的树中,这两个查询有效,但我想要类别 - &gt;子类别 - &gt;项目----&gt;子项

以下是模型的示例片段

enter image description here

如何正确加载?

1 个答案:

答案 0 :(得分:1)

绑定一个剑道树视图使用视图模型内置的kendo进行树视图调用

Kendo.Mvc.UI.TreeViewItemModel

这是解决方案

public ActionResult GetAllTreeData()
{
    var treeItems = new List<Kendo.Mvc.UI.TreeViewItemModel>();
    var categories = from category in this.dataContext.Categories
                     select category;
    var products = from Item in this.dataContext.Items
                   select Item;
    var allCategoryList = categories.toList();
    var allItems = products.ToList();

    if (allItems == null)
    {
        allItems = new List<Item>();
    }

    if (allCategoryList != null && allCategoryList.count() > 0)
    {
        foreach (var category in allCategoryList)
        {
            treeItems.Add(new Kendo.Mvc.UI.TreeViewItemModel()
            {
                Id = category.Id.ToString(),
                Text = category.Name
            });
        }

        foreach (var node in treeItems)
        {
            var items = products.Where(x => x.CategoryId == Convert.ToInt32(node.Id)).ToList();
            if (items.Any())
            {
                foreach (var item in items)
                {
                    var parentItem = new Kendo.Mvc.UI.TreeViewItemModel()
                    {
                        Id = item.Id.ToString(),
                        Text = item.Name
                    };

                    this.BuildTreeRecursive(allItems, parentItem);
                    treeItems.Add(parentItem);
                }
            }
        }
    }
}

public void BuildTreeRecursive(IEnumerable<Item> allItems, Kendo.Mvc.UI.TreeViewItemModel parentNode)
{
    parentNode.HasChildren = true;

    var nodes = allItems
        .Where(x => x.ParentItemId == Convert.ToInt32(parentNode.Id))
        .Select(p => new TreeViewItemModel 
        { 
            Text = p.Name, 
            Id = p.Id.ToString(CultureInfo.InvariantCulture) 
        });
    foreach (var node in nodes)
    {
        parentNode.Items.Add(node);
        this.BuildTreeRecursive(allItems, node);
    }
}