0

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?

1 Answer 1

2

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.