bootstrap treeview子节点数据绑定问题

时间:2014-11-04 08:03:29

标签: c# jquery node.js twitter-bootstrap treeview

我的MVC4网络应用程序中有一个bootstrap树视图控件。它工作正常

在第一级树节点绑定中。但是它没有工作的第二级节点。

请参阅下面的代码。

JS

function BuildTreenodes() {

    var url = '@Url.Action("GetInventoryParameters", "Admin")';

    $.get(url, function (graphdata) {

        var TreeNodes = new Array();

        $.each(graphdata, function (key, value) {

            $.each(value.Nodes, function (key, value) {
                //new Array(_.object([['text', value.Name]]))
                alert("key : " + value.Name);
            });

            TreeNodes.push({
                text: value.Name,
                tags: new Array("LEVEL:" + value.Level, "ID:" + value.Id),

                nodes: new Array(
                                _.object(
                                            [
                                                ['text', value.Name],
                                                ['tags', new Array(
                                                        "LEVEL:" + "2nd",
                                                        "ID:" + value.Id
                                                    )
                                                ],
                                                ['nodes', new Array(
                                                                    _.object(
                                                                                [
                                                                                    ['text', value.Name],
                                                                                    ['tags', new Array(
                                                                                            "LEVEL:" + "3rd",
                                                                                            "ID:" + value.Id
                                                                                        )
                                                                                    ],
                                                                                    ['nodes', new Array(
                                                                                                        _.object(
                                                                                                                    [
                                                                                                                        ['text', value.Name],
                                                                                                                        ['tags', new Array(
                                                                                                                                "LEVEL:" + "4th",
                                                                                                                                "ID:" + value.Id
                                                                                                                            )
                                                                                                                        ],
                                                                                                                    ]
                                                                                                                )
                                                                                                      )
                                                                                    ]
                                                                                ]
                                                                            )
                                                                  )
                                                ]
                                            ]
                                        )
                                )

            });

            $("#treeview12").treeview({ data: TreeNodes });

        });
    });
}

动作

    public JsonResult GetInventoryParameters()
    {

        Entities db = new Entities();

        List<InventoryParameterModel> parameters = new List<InventoryParameterModel>();

        /* Get segments */
        parameters = db.InventoryParameters.Where(e => e.ParentId < 1).ToList().Select(x =>
                           new InventoryParameterModel
                           {
                               Id = x.Id,
                               Name = x.Name,
                               Level = "S",
                               ParentId = x.ParentId,
                               CreatedOn = x.CreatedOn,
                               CreatedBy = x.CreatedBy,
                               Nodes = db.InventoryParameters.Where(l => l.ParentId == x.Id).ToList().Select(l =>
                                             new InventoryParameterModel
                                             {
                                                 Id = l.Id,
                                                 Name = l.Name,
                                                 ParentId = l.ParentId,
                                                 CreatedOn = l.CreatedOn,
                                                 CreatedBy = l.CreatedBy,
                                                 Nodes = db.InventoryParameters.Where(t => t.ParentId == l.Id).ToList().Select(t =>
                                                    new InventoryParameterModel
                                                    {
                                                        Id = t.Id,
                                                        Name = t.Name,
                                                        ParentId = t.ParentId,
                                                        CreatedOn = t.CreatedOn,
                                                        CreatedBy = t.CreatedBy,
                                                        Nodes = db.InventoryParameters.Where(r => r.ParentId == t.Id).ToList().Select(r =>
                                                           new InventoryParameterModel
                                                           {
                                                               Id = r.Id,
                                                               Name = r.Name,
                                                               ParentId = r.ParentId,
                                                               CreatedOn = r.CreatedOn,
                                                               CreatedBy = r.CreatedBy,
                                                               Nodes = null
                                                           }).ToList()
                                                    }).ToList()
                                             }).ToList()
                           }).ToList();


        return Json(parameters, JsonRequestBehavior.AllowGet);

    }

模型

    public partial class InventoryParameterModel
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Level { get; set; }
        public long ParentId { get; set; }
        public System.DateTime CreatedOn { get; set; }
        public long CreatedBy { get; set; }
        public List<InventoryParameterModel> Nodes { get; set; }
    }

结果

enter image description here

我使用underscore.js来构建此树视图。我的问题是如何传递子节点

jQuery中的名字。

请帮助。

0 个答案:

没有答案