您好我正在尝试将 Kendo UI Treeview 绑定到 ASP.NET MVC 4 控制器。我已尝试使用远程数据跟踪TreeViews上的 Kendo UI 演示中的信息。据我所知,返回的 JSON 似乎是正确的,数据数组周围的封装对象是否存在问题?
查看代码
<div class="treeview-back" style="padding: 10px">
<h3>Groups</h3>
@(Html.Kendo().TreeView()
.Name("treeviewGroup")
.DataTextField("Name")
.DataSource(dataSource => dataSource
.Read(read => read
.Action("GroupsRead", "Group")
)
)
)
</div>
控制器
public class GroupController : Controller
{
private IUserService _userService;
public GroupController(IUserService userService)
{
_userService = userService;
}
//
// GET: /GroupManage/
public ActionResult ManageGroup()
{
return View();
}
public ActionResult GroupsRead([DataSourceRequest]DataSourceRequest request)
{
var groups = _userService.GetAllGroups();
List<GroupTreeViewModel> treeViewGroups = new List<GroupTreeViewModel>();
foreach (var group in groups)
{
treeViewGroups.Add(new GroupTreeViewModel()
{
id = group.Id,
Description = group.Description,
Name = "Test Node",
text = "Body of test node",
hasChildren = false,
ParentId = null
}
);
}
return Json(treeViewGroups.ToDataSourceResult(request),JsonRequestBehavior.AllowGet);
}
}
**返回的 Json
{"Data":[{"Name":"Test Node","Description":"Full","text":"Body of test node","id":1,"hasChildren":false,"ParentId":null},{"Name":"Test Node","Description":"Platform 1","text":"Body of test node","id":2,"hasChildren":false,"ParentId":null}],"Total":2,"AggregateResults":null,"Errors":null}
更新
我解决了这个问题。返回的 json 数据的布局与树视图所期望的不同。树视图需要一个节点数组,而不是我发送包裹在另一个对象中的那个数组。
我通过删除对 .ToDataSourceRequest() 的调用来解决此问题
return Json(treeViewGroups,JsonRequestBehavior.AllowGet);