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
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 in开发者_运维知识库stead).
What is the best way to get such functionality in ASP.NET MVC?
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());
}
精彩评论