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>