Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 126.8k

dynamic foreach loop on parent child combination in MVC.net

Sep 6 2016 7:56 AM
i have a parent id and child id...this child id is also the parent of some child..i.e. parent having multiple child..and these multiple child having parent of some child..
example:Suppose A is the parent of B,C,D and E...if B has some child name G,H,I and G has some child having J,K..now how to use foreach loop here in this case???
now how to use foreach loop???
Here is the code but it works for only single parent child relationship(suppose A having B,C,D child):
enter code here
  1. @model Abacus_CMS.Models.AbacusModel  
  2. @{  
  3. Layout = null;  
  4. //for parent  
  5. var ParentCatagoryList = Model.AbacusMenuList.Where(x => x.ParentCatagoryId == 0).ToList();  
  6. //for parentchild  
  7. var ParentChildMenuList = Model.AbacusChildMenuList.Where(x => x.ChildParentId != 0).ToList();  
  8. }  
  9. @*//add sidebar parent and their childname*@  
  10. @if (ParentCatagoryList.Count > 0)  
  11. {  
  12. <ul class="page-sidebar-menu " data-auto-scroll="true" data-slide-speed="200">  
  13. <li class="sidebar-toggler-wrapper">  
  14. <!-- BEGIN SIDEBAR TOGGLER BUTTON -->  
  15. <div class="sidebar-toggler">  
  16. </div>  
  17. <!-- END SIDEBAR TOGGLER BUTTON -->  
  18. </li>  
  19. @foreach (var item in ParentCatagoryList)  
  20. {  
  21. <li>  
  22. <a href="@Url.RouteUrl("GettingStarted", new {catname =HttpUtility.UrlEncode(item.Name.Replace(' ', '-')) })" id="@item.Name.Replace(' ', '-').ToLower()">  
  23. <i class="icon-user"></i>  
  24. <span class="title" style="margin-left: -24px;">@item.Name</span>  
  25. @*<span class="arrow " style="height: 4px;"></span>*@  
  26. <span class="icon-01"style="float: left;  
  27. margin-top: -2px;  
  28. margin-left: 20px;">  
  29. <i class="fa fa-angle-right" aria-hidden="true"></i>  
  30. </span>  
  31. </a>  
  32. <ul class="sub-menu">  
  33. @foreach (var childitem in ParentChildMenuList)  
  34. {  
  35. if (item.Id == childitem.ChildParentId)  
  36. {  
  37. <li style="margin-left: 38px;">  
  38. <span style="background: #999999; width: 4px; height: 4px; float: left; margin-left: -20px; margin-top: 13px;"></span>  
  39. <a href="@Url.RouteUrl("GettingStarted", new { catname = HttpUtility.UrlEncode(childitem.ChildName.Replace(' ', '-'))})" id="@childitem.ChildName.Replace(' ', '-').ToLower()">@childitem.ChildName</a>  
  40. </li>  
  41. }  
  42. }  
  43. </ul>  
  44. </li>  
  45. }  
  46. </ul>  
  47. }  
here one loop i get parent and another loop i get child...
soo for multiple combination what should i do??

Answers (1)