Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my project i want to display menu in layout page for that i use viewbag but i don't know how to use viewbag data in recursive function in view for menu and sub menu generation.
Posted

1 solution

Try below code:
HTML
@
{
	ShowMenu((IEnumerable<menu>)ViewBag.MenuObjectList);
}

@helper ShowMenu(IEnumerable<menu> menuList)
{
    <ul>
        @foreach (var menuObj in menuList)
        {
            <li>
                @menuObj.Title
                @if (menuObj.Children.Any())
                {
                    @ShowMenu(menuObj.Children)
                }
            </li>
        }
    </ul>
}</menu></menu>

Here Menu is class and MenuObjectList is ViewBag parameter- you need to set inside contriller.
 
Share this answer
 
Comments
Member 11930887 28-Sep-15 8:09am    
what is children??
[no name] 28-Sep-15 8:11am    
Children is your sub menulist and you need change as per you need. I just a demo code to you.

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