使用服务器端包装器的带有分层数据源的Kendo TreeView

时间:2013-04-26 14:14:10

标签: asp.net-mvc treeview kendo-ui datasource

如何使用MVC ServerSide包装器创建HierarchialDataSource并将其用作TreeView的DataSource?

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

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

感谢您的帮助。

2 个答案:

答案 0 :(得分: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属性)。

不确定这是否是正确的做法(仍然在寻找官方信息),但至少它是有效的。

答案 1 :(得分: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; } }
}