Asp.net mvc }, “json_数据”:{ “ajax”:{ “url”:“/Controller/TreeViewTestContent”, “数据”:功能(n){ 返回{id:n.attr?n.attr(“id”):0}; } } }, “插件”:[“主题”、“json_数据”、“ui”] }).bind(“select_node.jstree”,函数(事件、数据){ 警报(data.rslt.obj.attr(“id”); 警报(data.rslt.obj.attr(“其他属性”); }); });

Asp.net mvc }, “json_数据”:{ “ajax”:{ “url”:“/Controller/TreeViewTestContent”, “数据”:功能(n){ 返回{id:n.attr?n.attr(“id”):0}; } } }, “插件”:[“主题”、“json_数据”、“ui”] }).bind(“select_node.jstree”,函数(事件、数据){ 警报(data.rslt.obj.attr(“id”); 警报(data.rslt.obj.attr(“其他属性”); }); });,asp.net-mvc,asp.net-mvc-3,jquery,jstree,Asp.net Mvc,Asp.net Mvc 3,Jquery,Jstree,提供树状视图数据的控制器: public JsonResult TreeViewTestContent(string id) TreeViewItemModel aItem = new TreeViewItemModel(); aItem.data = new TreeViewItemModelData { title = "Root Node",

提供树状视图数据的控制器:

public JsonResult TreeViewTestContent(string id)
        TreeViewItemModel aItem = new TreeViewItemModel();
        aItem.data = new TreeViewItemModelData
                     {
                         title = "Root Node", 
                         icon = "folder"
                     };
        aItem.attr = new TreeViewItemModelAttributes
                     {
                         id = "1",
                         other_attribute = "additional data can go here"
                     };
        aItem.state = "open";          

        List<TreeViewItemModel> aChildrenlist = new List<TreeViewItemModel>();
        TreeViewItemModel aChild = new TreeViewItemModel();
        aChild.data = new TreeViewItemModelData
        {
            title = "Child 1",
            icon = "folder",
        };
        aChild.attr = new TreeViewItemModelAttributes
                      {
                          id = "2",
                          other_attribute = "another value"
                      };
        aChildrenlist.Add(aChild);
        aItem.children = aChildrenlist;

        return Json(aItem,JsonRequestBehavior.AllowGet);
    }
publicJSONResult树视图测试内容(字符串id)
TreeViewItemModel aItem=新的TreeViewItemModel();
aItem.data=新的TreeViewItemModelData
{
title=“根节点”,
icon=“文件夹”
};
aItem.attr=新的TreeViewItemModelAttributes
{
id=“1”,
other\u attribute=“其他数据可以转到此处”
};
aItem.state=“打开”;
List aChildrenlist=新列表();
TreeViewItemModel aChild=新的TreeViewItemModel();
aChild.data=新的TreeViewItemModelData
{
title=“儿童1”,
icon=“文件夹”,
};
aChild.attr=新的TreeViewItemModelAttributes
{
id=“2”,
其他属性=“另一个值”
};
aChildrenlist.Add(aChild);
aItem.children=aChildrenlist;
返回Json(aItem,JsonRequestBehavior.AllowGet);
}
模型呢

public class TreeViewItemModel
{
    public TreeViewItemModelData data { get; set; } 
    public string state { get; set; } //'open' to display node children when loaded, 'closed' to make an AJAX call to retrieve the children, 'string.empty' to not display the child nodes when loaded and do not make ajax calls to get the children
    public TreeViewItemModelAttributes attr { get; set; }
    public List<TreeViewItemModel> children { get; set; }
}

public class TreeViewItemModelData
{
    public string title { get; set; } //text do be displayed in the node
    public string icon { get; set; } //remove this property if not wanting to customize node icon

}

public class TreeViewItemModelAttributes
{
    public string id { get; set; }
    public string other_attribute { get; set; }
}
公共类TreeViewItemModel
{
public TreeViewItemModelData数据{get;set;}
公共字符串状态{get;set;}//'open'用于在加载时显示节点子节点,“closed”用于进行AJAX调用以检索子节点,“string.empty”用于在加载时不显示子节点,并且不进行AJAX调用以获取子节点
public TreeViewItemModelAttributes属性{get;set;}
公共列表子项{get;set;}
}
公共类TreeViewItemModelData
{
公共字符串标题{get;set;}//文本不会显示在节点中
公共字符串图标{get;set;}//如果不想自定义节点图标,请删除此属性
}
公共类TreeViewItemModelAttributes
{
公共字符串id{get;set;}
公共字符串其他_属性{get;set;}
}
希望这对任何使用jstree的人来说都是一个很好的起点

    <div id="demo1"></div>
    <script type="text/javascript">
        $(function () {
            $("#demo1").jstree({
                "themes": {
                    "theme": "classic",
                    "dots": false
                },
                "json_data": {
                    "ajax": {
                        "url": "/Controller/TreeViewTestContent",
                        "data": function (n) {
                            return { id: n.attr ? n.attr("id") : 0 };
                        } 
                    }
                },
                "plugins": ["themes", "json_data", "ui"]
            }).bind("select_node.jstree", function (event, data) {
                alert(data.rslt.obj.attr("id"));
                alert(data.rslt.obj.attr("other_attribute"));

            });
        });
    </script>
public JsonResult TreeViewTestContent(string id)
        TreeViewItemModel aItem = new TreeViewItemModel();
        aItem.data = new TreeViewItemModelData
                     {
                         title = "Root Node", 
                         icon = "folder"
                     };
        aItem.attr = new TreeViewItemModelAttributes
                     {
                         id = "1",
                         other_attribute = "additional data can go here"
                     };
        aItem.state = "open";          

        List<TreeViewItemModel> aChildrenlist = new List<TreeViewItemModel>();
        TreeViewItemModel aChild = new TreeViewItemModel();
        aChild.data = new TreeViewItemModelData
        {
            title = "Child 1",
            icon = "folder",
        };
        aChild.attr = new TreeViewItemModelAttributes
                      {
                          id = "2",
                          other_attribute = "another value"
                      };
        aChildrenlist.Add(aChild);
        aItem.children = aChildrenlist;

        return Json(aItem,JsonRequestBehavior.AllowGet);
    }
public class TreeViewItemModel
{
    public TreeViewItemModelData data { get; set; } 
    public string state { get; set; } //'open' to display node children when loaded, 'closed' to make an AJAX call to retrieve the children, 'string.empty' to not display the child nodes when loaded and do not make ajax calls to get the children
    public TreeViewItemModelAttributes attr { get; set; }
    public List<TreeViewItemModel> children { get; set; }
}

public class TreeViewItemModelData
{
    public string title { get; set; } //text do be displayed in the node
    public string icon { get; set; } //remove this property if not wanting to customize node icon

}

public class TreeViewItemModelAttributes
{
    public string id { get; set; }
    public string other_attribute { get; set; }
}