Misha Narinsky
Misha Narinsky

Reputation: 687

ASP.NET MVC Create dynamic navigation sub-menu on the master page

I'm trying to create an ASP.NET MVC master page so the site navigation on it will look like this:

Main Menu:Home | About | News
Sub Menu: Home_Page1 | Home_Page2

The Sub Menu section should always show sub-menu for the currently selected Main Menu page (on the example above 'Home' page is selected) unless a user hovers the mouse on another Main Menu item (then it shows that item's sub-menu instead).

What is the best way to get such functionality in ASP.NET MVC?

Upvotes: 0

Views: 9193

Answers (1)

Mike Glenn
Mike Glenn

Reputation: 3510

If you create the menus using an HtmlHelper Extension method like:

<%= Html.RenderMenu() %>

you can use the HtmlHelper instance to look at the request context and determine what page you are on. Once you have this you can lookup (database, configuration, where ever your menu data is...) which submenu to render.

Heres something to get you point in the direction I think you are looking for:

public static MvcHtmlString RenderMenu(this HtmlHelper html) {
            var somePage = Html.ViewContext.HttpContext.Request.RawUrl;
            var menu = lookupMenuBasedOnPage(somePage);

            return MvcHtmlString.Create(menu.Render());
}

Upvotes: 2

Related Questions