Active menu item - asp.net mvc3 master page?

A custom HTML helper usually does the job fine.

A custom HTML helper usually does the job fine: public static MvcHtmlString MenuLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName ) { string currentAction = htmlHelper.ViewContext.RouteData. GetRequiredString("action"); string currentController = htmlHelper.ViewContext.RouteData. GetRequiredString("controller"); if (actionName == currentAction && controllerName == currentController) { return htmlHelper.

ActionLink( linkText, actionName, controllerName, null, new { @class = "current" }); } return htmlHelper. ActionLink(linkText, actionName, controllerName); } and in your master page: @Html. MenuLink("Link 1", "link1", "Home") @Html.

MenuLink("Link 2", "link2", "Home") Now all that's left is define the . Current CSS class.

6 Worth noting htmlHelper.ActionLink() needs "using System.Web.Mvc. Html;" – Kohan Feb 8 at 17:19 Works brilliantly +1 – Kohan Feb 8 at 17:26 1 You also need to import the namespace in your view, if using Razor in MVC3 you can do this by simply adding @using to your view – Duncan Apr 24 at 12:53 I had one problem with that. What if in "link2" page I have a link to "link3" which points to another controller and action but it is a part of the "link2" section.

The "link2" is no longer active as controller and action in request does not match what we pass to the helper. I have worked out a clean solution: no sessions, no ugly code in views. Arturito.Net/2011/08/03/… The extra part is a dictionary with route mappings.

– Arturito Aug 4 at 6:50.

Here is my solution of this issue. I created following extension method of HtmlHelpers class: public static class HtmlHelpers { public static string SetMenuItemClass(this HtmlHelper helper, string actionName) { if (actionName == helper.ViewContext.RouteData. Values"action".ToString()) return "menu_on"; else return "menu_off"; } Then I have my menublock.It looks like this: @Html.

ActionLink("About", "About", "Home") @Html. ActionLink("Prices", "Prices", "Home") So my method returns class name to every div according to current action of Home controller. You can go deeper and add to the method one parameter, which specifies the name of the controller to avoid problems, when you have actions with the same name but of different controllers.

What I usually do is assign a class to the body tag that's based on the path parts. So like, if you do a String. Replace on the path to turn /blogs/posts/1 to class="blogs posts 1".

Then, you can assign CSS rules to handle that. For example, if you have a menu item for "blogs", you can just do a rule like BODY. Blogs li.

Blogs { /* your style */} or if you want a particular style if your on a post only vice if you're on the blog root page BODY.blogs. Posts li. Blogs {/* your style */}.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions