2014-07-19 104 views
6

我正在使用MVC 5和ASP.NET MVC的Telerik(以前称为Kendo)UI的最新版本。我正在处理分层数据。我正在尝试在_Layout视图中创建TreeView并使用URL或操作链接填充它。ASP.NET MVC TreeView的Telerik(Kendo)UI问题

我当前的代码:

在_layout查看:

@Html.Partial("_ProductTree") 

“_ProductTree” 管窥:

@(Html.Kendo().TreeView().Name("ProductTree") 
    .DataSource(d => d 
     .Model(m => m 
      .Id("ProductId") 
      .HasChildren("Categories")) 
    .Read(r => r.Action("_ProductTree", "Home"))) 
    .DataTextField("ProductName")) 

操作方法:

[ActionName("_ProductTree")] 
public JsonResult GetProductTree() 
{ 
    var products = _productBusinessService.GetProducts(); 

    var result = products.Select(p => new 
    { 
     p.ProductID, 
     p.ProductName, 
     Categories= c.Categories.Any() 
    }).OrderBy(t => t.ProductName); 

    return Json(result, JsonRequestBehavior.AllowGet); 
} 

我有一些发行es:

  1. 当我展开具有子级的父节点时,TreeView正在触碰操作方法并将整个树附加到子级,而不是仅显示子级。
  2. 我需要能够嵌套TreeView双深,例如Product> Category> Type。
  3. 我想弄清楚如何使用LINQ来聚合或投影数据来做一个双深度高层次的
  4. 我试着关闭LoadOnDemand,但是这使得TreeView在Product列表中为每个记录调用一次action方法。

我已经尝试将TreeView Razor代码直接插入_Layout视图(不使用局部视图)。我意识到我可能需要将操作方法​​移到基本控制器类中,并在每个控制器中继承它,以阻止它将列表附加到父节点。如果我不能很快得到这个工作,我可能不得不使用Kendo UI Professional或开源替代品。

你们中的一些人可能会试图说这个问题已经在别处得到了回答。 我发现的帖子都没有解决使用Telerik TreeView填充和显示嵌套(超过一个深度)分层数据的问题。

有这个帖子

Nested DataSources and TreeView with kendo ui

但它是树视图(不MVC用户界面)版的JavaScript版本,也没有得到答复。

非常感谢您的帮助!

回答

4

在代码中的溶液:

的_layout视图:

@Html.Partial("_ProductTree") 

@RenderSection("productTree", false) 

然后在内容视图

@section productTree 
{ 
    @Html.Partial("_ProductTree") 
} 

的_ProductTree局部视图

@(Html.Kendo().TreeView().Name("ProductTree") 
    .DataSource(d => d 
     .Model(m => m 
      .Id("Id") 
      .HasChildren("HasChildren") 
      .Children("Children")) 
    .Read(r => r.Action("_ProductTree", "Home"))) 
    .DataTextField("Name")) 

我将操作方法​​移到了一个BaseController抽象类,该类可以被需要显示ProductTree TreeView的任何控制器继承。该数据是从ProductService和CategoryService拉并汇总使用LINQ投影到匿名对象:

[ActionName("_ProductTree")] 
    public JsonResult GetProductData() 
    { 
     var products = _productBusinessService.GetProducts(); 

     foreach (var product in product) 
     { 
      foreach (var category in product.Categories) 
      { 
       category.ProductTypes = 
        _productService.GetProductTypes(category.CategoryId);           
      } 
     } 

     var productTreeData = products.Select(p => new 
     { 
      Id = p.ProductId, 
      Name = p.ProductName, 
      HasChildren = p.Categories.Any(), 
      Children = p.Categories.Select(c => new 
      { 
       Id = c.CategoryId, 
       Name = c.CategoryName, 
       HasChildren = c.ProductTypes.Any(), 
       Children = c.ProductTypes.Select(t => new 
       { 
        Id = t.ProductTypeId, 
        Name = t.ProductTypeName, 
        HasChildren = false 
       }).OrderBy(t => t.Name).ToList() 
      }).OrderBy(c => c.Name).ToList() 
     }).OrderBy(p => p.Name).ToList(); 

     return Json(productTreeData, JsonRequestBehavior.AllowGet); 
    } 

结果是一个包含名称和ID 3深,完全填充,整理Telerik的UI为ASP.NET树形产品>>类别>>产品类型。打开或关闭LoadOnDemand在这种情况下似乎没有什么区别。在TreeView中使用绑定时应该有所作为。

我希望这有助于!

+0

为什么。模型是未知的? 'Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder'不包含'Model'的定义,也没有扩展方法'Model' –