我刚刚将 jqTree 添加到我的 ASP MVC 应用程序中。我需要在我的视图中显示一个 TreeView:
@{
ViewBag.Title = "Tree";
}
<h2>@ViewBag.Title</h2>
<div id="tree1" data-url="/Home/Nodes"></div>
@section scripts {
<script language="javascript" type="text/javascript">
$(function () {
$('#tree1').tree();
});
</script>
}
我的数据在 JSON 文件 (~/App_Data/Roles.json) 中:
{
"id": 1,
"label": "Role1",
"children": [
{
"id": 2,
"label": "Role2",
"children": [
{
"id": 3,
"label": "Role3",
"children": [
]
},
{
"id": 4,
"label": "Role4",
"children": [
]
}
]
}
]
}
如何在控制器动作方法中加载json文件以在视图中显示对应的TreeView?
public ActionResult Nodes()
{
var roles = // load json file
return Json(roles, JsonRequestBehavior.AllowGet);
}