Solution in code:
_Layout view:
@Html.Partial("_ProductTree")
or
@RenderSection("productTree", false)
then in content view
@section productTree { @Html.Partial("_ProductTree") }
partial view _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"))
I moved the action method to the abstract BaseController class, which can be inherited by any controller that needs to display the ProductTree TreeView. The data was pulled from ProductService and CategoryService and aggregated using LINQ projection into anonymous objects:
[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); }
The result is a three-dimensional, fully populated, sorted Telerik user interface for ASP.NET Treeview, containing product names and identifiers β Category β ProductType. Turning the LoadOnDemand function on and off did not help in this case. This should make a difference when using Bind in a TreeView.
Hope this helps!
source share