Automatic highlight menu for asp.net mvc.
For a while now I've been wondering how to work out with the menu, so that it can automatic highlight with the least amount of code. So after a while, i come up with this. There are some part which I used the language helper I wrote in previous post. Just go ahead and replace it to whatever that suit your needs.
This one i place on top of my Partial that contain the menu to get the action and controller, so that i can pass this to the extension.
< % string currentAction = ViewContext.RouteData.Values["action"].ToString(); string currentController = ViewContext.RouteData.Values["controller"].ToString(); %>
This is the sidebar Item, basically this will generate a "li" tag with a link and your custom class to indicate whether the link is currently used in the page / highlight. There is a if check in this method, it check for both current action and method if it actually fit the link.
so if you are doing a higher level menu item, in this case, a controller menu, you can just simply compare the controller instead of both.
1 2 3 4 5 6 7 8 9 10 11 12 | public static string SidebarItem(this System.Web.Mvc.HtmlHelper html, string currentAction, string currentController, string action, string controller, string languageKey, params object[] args) { TagBuilder tb = new TagBuilder("li"); if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) && string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase)) { tb.GenerateId("activemenu"); } string text = html.Language(languageKey, args); string link = html.ActionLink(text, action, controller).ToHtmlString(); tb.SetInnerText("{0}"); return String.Format(tb.ToString(), "<span>"+link+"</span>"); } |
And here is the actual sample usage of the code above:
< %= Html.SidebarItem(currentAction, currentController, "index", "home", "index") %>
This help a lot when you create a menu that can automatic indicate whether you are in that page or not.
Licenses Status
VBB 4 Available: 0
VBB 4 Sold: 3
VBB 4 Suite Available: 0
VBB 4 Suite Sold: 9
VBB 3 Available: 0
VBB 3 Sold: 6
VbSeo Available: 0
VBSeo Sold: 4
IPB Available: 5
IPB Sold: 4
Recent Posts
- Jquery collapsible menu for WordPress
- New VBSEO License stock.
- New VBB Stock as well as IPB
- Out of stock on ALL Vbulletin licenses. Openning IPB sale.
- AT&T network exploit (seem like it)
Recent Comments
- affintyfara on MkDir recursively with SharpSSH SFTP
- Connor Campbell on Debate on abortion
- Jayna Willinghurst on
- [Tip] - Chuyển DNS để vào facebook bằng vài nhấp chuột. | Tip4PC on Automatic change DNS for Facebook
- Jason S. on Automatic highlight menu for asp.net mvc.
Categories
- Blog (32)
- Programing (12)
- Asp.net MVC (8)
- Python (1)
- SQL (3)
Tags
Blogroll
Spam cleared
Who's Online
- 0 Members
- 4 Guests
January 14th, 2010 - 11:01
very interesting article
June 17th, 2010 - 06:12
Just what I needed to get me looking in the right place. Good show!