JSTree 添加节点到子节点

JSTree adding nodes to child nodes

我正在将 JSTree 与 MVC 项目一起使用,并且我正在尝试将子节点添加到树中,但是我收到空引用错误对象引用未设置到行上的对象实例 subGroupNode.children.Add(itemNode); 我猜这是因为 subGroupNode.Children 是空的。怎么会在之前的foreach循环中创建child的时候呢。

    public JsonResult GetJsTree3Data()
    {
        var marketGroups = new List<JsTree3Node>();

        // Create our root node and ensure it is opened
        var root = new JsTree3Node()
        {
            id = Guid.NewGuid().ToString(),
            text = "Market Items",
            state = new State(true, false, false)
        };

        foreach (var group in GenerateGroups(connString))
        {
            if (group.marketParentGroup == 0)
            {
                var node = JsTree3Node.NewNode(group.id_str);
                node.text = group.name;
                node.state = new State(false, false, false);
                marketGroups.Add(node);
            }
        }

         foreach (var marketGroup in marketGroups)
        {
            foreach (var subGroup in GenerateGroups(connString))
            {
                if (subGroup.marketParentGroup.ToString() == marketGroup.id)
                {
                    var childNodes = new List<JsTree3Node>();

                    var childNode = new JsTree3Node();
                    childNode.id = subGroup.id_str;
                    childNode.text = subGroup.name;
                    childNode.state = new State(false, false, false);

                    childNodes.Add(childNode);

                    var subGroupNode = new JsTree3Node();
                    subGroupNode.id = subGroup.id_str;
                    subGroupNode.text = subGroup.name;
                    subGroupNode.state = new State(false, false, false);
                    subGroupNode.children = childNodes;

                    marketGroup.children.Add(subGroupNode);
                }
            }
        }

        return Json(root, JsonRequestBehavior.AllowGet);
    }

我通过创建与第一个相同的第二个列表并比较这两个列表解决了这个问题。

 public JsonResult GetJsTree3Data()
    {
        var marketGroups = new List<JsTree3Node>();
        var marketSubGroups = new List<JsTree3Node>();

        foreach (var group in GenerateGroups(connString))
        {
            var node = JsTree3Node.NewNode(group.id_str);
            node.text = group.name;
            node.state = new State(false, false, false);
            node.parentId = group.marketParentGroup;
            marketSubGroups.Add(node);
        }

        // Create our root node and ensure it is opened
        var root = new JsTree3Node()
        {
            id = Guid.NewGuid().ToString(),
            text = "Market Items",
            state = new State(true, false, false)
        };

        foreach (var group in marketSubGroups)
        {
                var node = JsTree3Node.NewNode(group.id);
                node.text = group.text;
                node.state = new State(false, false, false);
                marketGroups.Add(node);
        }

        foreach (var marketGroup in marketGroups)
        {
            foreach (var subGroup in marketSubGroups)
            {
                if (subGroup.parentId.ToString() == marketGroup.id)
                {
                    var subGroupNode = new JsTree3Node();
                    subGroupNode.id = subGroup.id;
                    subGroupNode.text = subGroup.text;
                    subGroupNode.state = new State(false, false, false);
                    marketGroup.children.Add(subGroupNode);
                }
            }
        }

        root.children = marketGroups;

        return Json(root, JsonRequestBehavior.AllowGet);
    }