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
- @model Abacus_CMS.Models.AbacusModel
- @{
- Layout = null;
- //for parent
- var ParentCatagoryList = Model.AbacusMenuList.Where(x => x.ParentCatagoryId == 0).ToList();
- //for parentchild
- var ParentChildMenuList = Model.AbacusChildMenuList.Where(x => x.ChildParentId != 0).ToList();
- }
- @*//add sidebar parent and their childname*@
- @if (ParentCatagoryList.Count > 0)
- {
- class
- class
="sidebar-toggler-wrapper">- class="sidebar-toggler">
- @foreach (var item in ParentCatagoryList)
- {
- "@Url.RouteUrl("GettingStarted", new {catname =HttpUtility.UrlEncode(item.Name.Replace(' ', '-')) })" id="@item.Name.Replace(' ', '-').ToLower()">
- class="icon-user">
- class="title" style="margin-left: -24px;">@item.Name
- @*class="arrow " style="height: 4px;">*@
- class="icon-01"style="float: left;
- margin-top: -2px;
- margin-left: 20px;">
- class="fa fa-angle-right" aria-hidden="true">
- class
- @foreach (var childitem in ParentChildMenuList)
- {
- if (item.Id == childitem.ChildParentId)
- {
- "margin-left: 38px;"
>- "background: #999999; width: 4px; height: 4px; float: left; margin-left: -20px; margin-top: 13px;">
- "@Url.RouteUrl("GettingStarted", new { catname = HttpUtility.UrlEncode(childitem.ChildName.Replace(' ', '-'))})" id="@childitem.ChildName.Replace(' ', '-').ToLower()">@childitem.ChildName
- }
- }
- }
- }
here one loop i get parent and another loop i get child...
soo for multiple combination what should i do??
Saineshwar BageriPosted Sep 8, 2016, 2:58 AM
then create a static method [which take parentID as input ] by which passing parentID and then getting child records
@foreach (var parentitem in Model)
{
@{var childlist = demo.GetChildList(parentitem.ParentID); //Get GetChildList Method must be static
@foreach (var childitem in childlist)
{
}
}