0

我有一個存儲過程,它使用EF從SQL數據庫返回分層數據。 樹視圖僅顯示父節點,而不顯示子節點。我一直在研究這個問題一段時間,但沒有結果,令人沮喪。感謝任何幫助,我可以解決這個問題。來自MVC控制器的kendo ui treeview數據

ParentId Id   Title       Level 
--------- ---------- ----------------------------- ------ 
NULL  1   Chief Executive Officer  0 
1   273  Vice President of Sales  1 
273  16   Marketing Manager    2 
273  274  North American Sales Manager 2 
273  285  Pacific Sales Manager   2 
16  23   Marketing Specialist   3 
274  275  Sales Representative   3 
274  276  Sales Representative   3 
285  286  Sales Representative   3 

在服務層我有一個orchestrator類返回上面的表。

public IEnumerable<TreeviewItem> GetBusinessStructure() 
{ 
    return Context.ExecuteStoreQuery<TreeviewItem>("spGetEmployees"); 
} 

模型類

public class TreeviewItem 
{ 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public int? ParentId { get; set; } 
    public virtual TreeviewItem Parent { get; set; } 

    public virtual ICollection<TreeviewItem> Children { get; set; } 
} 

控制器

public JsonResult BusinessStructure(int? id) 
    { 
     var model = from e in  BusinessStructureOrchestrator.GetBusinessStructures() 
        where (id.HasValue ? e.ParentId == id : e.ParentId == null) 
        select new 
        { 
         id = e.Id, 
         Name = e.Name, 
         hasChildren = e.Children.Any() 
        }; 

     return Json(model, JsonRequestBehavior.AllowGet); 
    } 

視圖

@model FilterBusinessStructureViewModel 
<div class="form-group"> 

<label for="" class="col-sm-12 control-label">Choose Divisions/BUs/Sites</label> 

<div class="col-sm-12"> 
    @(Html.Kendo().TreeView() 
      .Name("tvBusinessStructure") 
      .DataTextField("Name") 
      .Checkboxes(checkedboxes => checkedboxes 
       .Name("SelectedDivision") 
       .CheckChildren(true) 
      ) 
      .DataSource(dataSource => dataSource 
       .Model(m => m 
        .Id("Id") 
        .HasChildren("hasChildren") 
        .Children("Children")) 
        .Read(read => read 
           .Action("BusinessStructure", "Report") 
        ) 
      ) 
     ) 
</div> 

回答

0

經過四處搜索,經過多次試驗和錯誤,我找到了一段代碼並對其進行了修改。用於遍歷層級中的類看起來像這樣

public static class Treeview 
{ 
    public static IList<TreeviewItem> BuildTree(this IEnumerable<TreeviewItem> source) 
    { 
     var groups = source.GroupBy(i => i.ParentId); 

     var roots = groups.FirstOrDefault(g => g.Key.HasValue == false).ToList(); 

     if (roots.Count > 0) 
     { 
      var dict = groups.Where(g => g.Key.HasValue).ToDictionary(g => g.Key.Value, g => g.ToList()); 
      for (int i = 0; i < roots.Count; i++) 
       AddChildren(roots[i], dict); 
     } 

     return roots; 
    } 

    private static void AddChildren(TreeviewItem node, IDictionary<int, List<TreeviewItem>> source) 
    { 
     if (source.ContainsKey(node.Id)) 
     { 
      node.Children = source[node.Id]; 
      for (int i = 0; i < node.Children.Count; i++) 
       AddChildren(node.Children[i], source); 
     } 
     else 
     { 
      node.Children = new List<TreeviewItem>(); 
     } 
    } 

} 

然後我作了如下改動的控制器方法

public JsonResult BusinessStructure(int? id) 
    { 

     List<BusinessStructureModel> businessStructures = BusinessStructureOrchestrator.GetBusinessStructures() 
     .Select(e => new BusinessStructureModel 
     { 
      Id = e.ID, 
      Name = e.Name, 
      ParentId = e.ParentID 
     }) 
     .BuildTree() 
     .ToList(); 

     return Json(businessStructures, JsonRequestBehavior.AllowGet); 
    }