When we use bootstrap nav-bar in asp.net MVC, we need to dynamically update the selected item class as active. By default this feature is not available in ASP.Net Mvc.
<ul class="nav navbar-nav"> <li class="active">@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> <li>@Html.ActionLink("Contact", "Contact", "Home")</li> </ul>
Solution :
We can achieve this by writing an extension method for ActionLink
public static class ActionHelper { public static MvcHtmlString LiActionLink(this HtmlHelper html, string text, string action, string controller) { var context = html.ViewContext; if (context.Controller.ControllerContext.IsChildAction) context = html.ViewContext.ParentActionViewContext; var routeValues = context.RouteData.Values; var currentAction = routeValues["action"].ToString(); var currentController = routeValues["controller"].ToString(); var str = string.Format("<li role=\"presentation\"{0}>{1}</li>", currentAction.Equals(action, StringComparison.InvariantCulture) && currentController.Equals(controller, StringComparison.InvariantCulture) ? " class=\"active\"" : String.Empty, html.ActionLink(text, action, controller).ToHtmlString() ); return new MvcHtmlString(str); } }
We need to use the extension method as action link
<ul class="nav navbar-nav"> <li>@Html.LiActionLink("Home", "Index", "Home")</li> <li>@Html.LiActionLink("About", "About", "Home")</li> <li>@Html.LiActionLink("Contact", "Contact", "Home")</li> </ul>
Thank you for sharing.
ReplyDeleteHelp me how to create a sub menu
In the event that you need to take in website architecture from home, there are a few alternatives open for you. These incorporate getting disconnected preparing through the different government or private organizations, or learning on the web by taking on the web classes and separation learning. Various elements will impact the choice you make on what preparing choice to take.
ReplyDeleteWeb Design Curriculum
Rattling clean internet site , thanks for this post. new york website design company
ReplyDeleteSome really nice stuff on this web site , I like it. website designers nyc
ReplyDeletegreat . Thanks for informations . Ill be back. Thanks again website designer nyc
ReplyDeleteThe subsequent time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I mean, I know it was my option to read, however I really thought youd have something attention-grabbing to say. All I hear is a bunch of whining about something that you possibly can repair when you werent too busy on the lookout for attention. web design agency new york
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHello! Nice post! Please do keep us posted when we can see a follow up! design agencies sf
ReplyDeleteVery nice design and style and fantastic subject matter, very little else we want : D. design agency san francisco
ReplyDeleteI just added this blog to my feed reader, great stuff. Cannot get enough! web designer san francisco
ReplyDeleteYou should take part in a contest for probably the greatest blogs on the web. I’ll recommend this web site! web designer san francisco
ReplyDeleteThank you, I have recently been searching for info about this subject for ages and yours is the best I’ve found so far. web designer la
ReplyDeleteUseful info. Fortunate me I discovered your website accidentally, and I am surprised why this coincidence didn’t happened earlier! I bookmarked it. facebook marketing
ReplyDeletethose polyurethane inflatable boats are very sturdy and some are kevlar lined too but they are expensive* web designer la
ReplyDeletethere is great money involve when we talk about personal injury, attorneys make great money out of them too,, la web design
ReplyDeleteThis is a great blog. and i want to visit this every day of the week , web designer los angeles
ReplyDeleteI think that may be an enchanting aspect, it made me assume a bit. Thank you for sparking my considering cap. Now and again I get such a lot in a rut that I just feel like a record. los angeles web agency
ReplyDeleteAdmiring the time and effort you put into your website and in depth information you offer. It’s good to come across a blog every once in a while that isn’t the same out of date rehashed material. Great read! I’ve saved your site and I’m including your RSS feeds to my Google account. website design
ReplyDeleteHello! Good stuff, please keep us posted when you post again something like that! website tips
ReplyDeletePossible require all types of led tourdates with some other fancy car applications. Many also provide historic packs and other requires to order take into your lending center, and for a holiday in upstate New York. ??? website tips
ReplyDeleteeCommerce Website Designers who are 100% Australian developers from Website Development Australia. We build high performing eCommerce websites. ecommerce
ReplyDeleteThanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. Swiss web agency
ReplyDeleteIn case you're actually considering outsourcing as an alternative, consider how in your life you can supplant the cooperation you'd get from work.Professional graphic design
ReplyDeleteExcellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. website redesign
ReplyDelete