2013-04-26 101 views
2

如何使用MVC ServerSide包装创建HierarchialDataSource并将其用作TreeView的数据源?Kendo TreeView与使用ServerSide包装的分层数据源

我已经创建了一个从控制器返回为Json的层次结构,但TreeView只显示顶级节点。是否需要在TreeView上设置一个属性来指示DataSource是否是heirarchial?

我看到了几个客户端使用JS和kendo.data库的例子,但我找不到服务器端等效的程序集。

感谢您的协助。

回答

1

从我的理解,你必须结合在HierarchicalDataSource匹配模型的属性: http://docs.kendoui.com/api/framework/hierarchicaldatasource

我使用树形视图来显示一个菜单结构,所以这里是我的模型:

public class Menu 
{ 
    public int Id { get; set; } 
    public int? ParentId { get; set; } 
    public string Description { get; set; } 

    public int id 
    { 
     get { return this.Id; } 
    }     

    public bool hasChildren { get; set; } 
} 

我明确必须实现idhasChildren属性与这个确切的大肠杆菌(我填充hasCHildren属性基于查询之前将模型推送到视图)。

不知道这是否是正确的做法(仍然徘徊于找到官方信息),但至少它的工作原理。

0

使用此在您的CSHTML文件:

@(Html.Kendo().TreeView() 
    .Name("trvReport") 
    .DataTextField("Name") 
    .DataSource(dataSource => dataSource 
     .Read(read => read.Action("ReportType", "Read")) 
     .Model(model => 
     { 
     model.Id("Id"); 
     model.Field("Name", typeof(string)); 
     model.Children("Reports"); 
     model.HasChildren("HasReports"); 
     }) 
    ) 
) 

在模型中实现HasChildren这样的:

public class ReportType 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public ICollection<Report> Reports { get; set; } 

    [NotMapped] 
    public bool HasReports { get { return Reports.Count() > 0; } } 
}