Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
dynamic foreach loop on parent child combination in MVC.net
Manoj Maharana
Sep 6 2016 7:56 AM
442
Reply
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)
{
<ul
class
=
"page-sidebar-menu "
data-auto-scroll=
"true"
data-slide-speed=
"200"
>
<li
class
=
"sidebar-toggler-wrapper"
>
<!-- BEGIN SIDEBAR TOGGLER BUTTON -->
<div
class
=
"sidebar-toggler"
>
</div>
<!-- END SIDEBAR TOGGLER BUTTON -->
</li>
@
foreach
(var item
in
ParentCatagoryList)
{
<li>
<a href=
"@Url.RouteUrl("
GettingStarted
", new {catname =HttpUtility.UrlEncode(item.Name.Replace(' ', '-')) })"
id=
"@item.Name.Replace(' ', '-').ToLower()"
>
<i
class
=
"icon-user"
></i>
<span
class
=
"title"
style=
"margin-left: -24px;"
>@item.Name</span>
@*<span
class
=
"arrow "
style=
"height: 4px;"
></span>*@
<span
class
=
"icon-01"
style="
float
: left;
margin-top: -2px;
margin-left: 20px;">
<i
class
=
"fa fa-angle-right"
aria-hidden=
"true"
></i>
</span>
</a>
<ul
class
=
"sub-menu"
>
@
foreach
(var childitem
in
ParentChildMenuList)
{
if
(item.Id == childitem.ChildParentId)
{
<li style=
"margin-left: 38px;"
>
<span style=
"background: #999999; width: 4px; height: 4px; float: left; margin-left: -20px; margin-top: 13px;"
></span>
<a href=
"@Url.RouteUrl("
GettingStarted
", new { catname = HttpUtility.UrlEncode(childitem.ChildName.Replace(' ', '-'))})"
id=
"@childitem.ChildName.Replace(' ', '-').ToLower()"
>@childitem.ChildName</a>
</li>
}
}
</ul>
</li>
}
</ul>
}
here one loop i get parent and another loop i get child...
soo for multiple combination what should i do??
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
how to add a checkbox on a grid in mvc4/5?/
listview pagenation