Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
am binding tree view from database ,i have 4 parents with their childs in my tree view am binding all parents with childs. But i want to bind only a particular parent with their childs , how to write query for that
My table

EmpID Name ReportsTo
1 Accounts 0
2 Branches 0
3 Loans 1
4 Incomes 2

Accounts, branches are parents loans incomes their childs
what is the query to return Accounts with childs

What I have tried:

html helper
HTML
@helper GetTreeView(List<AccountsPageLayout.MasterNode> siteMenu, int parentID)
{
    foreach (var i in siteMenu.Where(a => a.ReportsTo.Equals(parentID)))
    {
        <li>
            @{var submenu = siteMenu.Where(a => a.ReportsTo.Equals(i.EmpID)).Count();}
            @if (submenu > 0)
            {
                <span class="collapse collapsible"> </span>
            }
            else
            {
                <span style="width:15px; display:inline-block"> </span>
            }
            <span>
                <a href="#">@i.Name</a>

                @*<a href="@i.EmpID">@i.Name</a>*@
            </span>
            @if (submenu > 0)
            {
                <ul>
                    @Treeview.GetTreeView(siteMenu, i.EmpID)
                    @* Recursive  Call for Populate Sub items here*@
                </ul>
            }
        </li>
    }
}


view page

HTML
<div>
                        @if (Model != null && Model.Count() > 0)
                        {
                            <ul>
                                @Treeview.GetTreeView(Model, Model.FirstOrDefault().ReportsTo)
                            </ul>
                        }
                    </div>


controller
C#
List<MasterNode> all = new List<MasterNode>();
            using (TestDemoEntities dc = new TestDemoEntities())
            {
                all = dc.MasterNodes.OrderBy(a => a.ReportsTo).ToList();
            }
            return View(all);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900