Reputation: 687
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
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