MVC 3 Rendering 2-Level Menu as Partial View

Click Ahead picture Click Ahead · Feb 24, 2011 · Viewed 7.6k times · Source

I would like to implement a 2-level parent/child menu in my MVC 3 site such as

Company
 - Background
 - Contact

I have implemented a single, parent level menu as a PartialView like so ...

 <div id="menu" class="block">
    <ul id="menuItems">
        foreach (var item in Model)
        {
        <li id="@item.Id">@Html.ActionLink(item.Name, item.Action,item.Controller)</li>
        }
    </ul>
 </div>

and then included it on my MasterPage ...

 @{Html.RenderAction("MainMenu", "Menu");}

The problem is that I would like to render a second child-menu based on the menu item selected at the parent level. This involves passing the Id of the parent into the controller action that returns the menu model. I'm not sure how I can pass this parent Id into the controller action. Can anyone provide any insights into this? I'm using MVC3 & Razor.

Answer

Robert Dean picture Robert Dean · Apr 18, 2011

You may want to check out MvcSiteMapProvider which handles multilevel menu and sitemaps.