Hello everyone,
In MVC 4, How can I generating a treeview with recursive partial view ?
This is the sql database treeview structure.
I have tried,
- namespace Reboxwebapp.Models.ViewModel
- {
- public class ProductCategoryVM
- {
- public int idProductCategory { get; set; }
- public string ProductCategoryName { get; set; }
- public string ProductCategoryDescription { get; set; }
- public Nullable<int> idParentCategoryProduct { get; set; }
- public string AlternateNamesToSearch { get; set; }
- public Nullable<int> LevelID { get; set; }
- public string CompletePathName { get; set; }
- public byte[] PCImage { get; set; }
-
- }
-
- public class SeededProductCategies
- {
- public int? Seed { get; set; }
- public IList<ProductCategoryVM> Categories { get; set; }
- }
- }
- [HttpGet]
- public ActionResult GetSearchProduct()
- {
- ProductsBIL objProduct = new ProductsBIL();
- IList<ProductCategoryBL> lsRes = new List<ProductCategoryBL>();
- lsRes = objProduct.GetProductCategoryTree();
- IList<ProductCategoryVM> categories = new List<ProductCategoryVM>();
-
-
- foreach (var item in lsRes)
- {
- categories.Add(new ProductCategoryVM {idProductCategory =item.idProductCategory,idParentCategoryProduct = item.idParentCategoryProduct,ProductCategoryDescription = item.ProductCategoryDescription });
- }
- SeededProductCategies objSeededProductCategies = new SeededProductCategies { Seed = null, Categories = categories };
- return View(objSeededProductCategies);
- }
GetSearchProduct actionresult go to view is shown below,
- @model Reboxwebapp.Models.ViewModel.SeededProductCategies
-
-
- @Html.Partial("_TreeCategories", Model)
then partial view,
- @model Reboxwebapp.Models.ViewModel.SeededProductCategies
-
- @if (Model.Categories.Where(s => s.idParentCategoryProduct == Model.Seed).Any()) /*But here is occurring error : ilist does not contain a definition for where cause*/
- {
- <ul>
- @foreach (var node in Model.Categories)
- {
- if (node.idParentCategoryProduct == Model.Seed)
- {
- Reboxwebapp.Models.ViewModel.SeededProductCategies inner = new Reboxwebapp.Models.ViewModel.SeededProductCategies { Seed = node.idProductCategory, Categories = Model.Categories };
- <li>
- <a href="[email protected]">@node.ProductCategoryDescription</a>
- @Html.Partial("_TreeCategories", inner)
- </li>
- }
- }
- </ul>
- }
How can I do this? Please help me.