Wednesday, January 20, 2016

Add active class to HTML in ASP.Net MVC


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>

24 comments:

  1. Thank you for sharing.
    Help me how to create a sub menu

    ReplyDelete
  2. 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.
    Web Design Curriculum

    ReplyDelete
  3. Rattling clean internet site , thanks for this post. new york website design company

    ReplyDelete
  4. Some really nice stuff on this web site , I like it. website designers nyc

    ReplyDelete
  5. great . Thanks for informations . Ill be back. Thanks again website designer nyc

    ReplyDelete
  6. The 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

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello! Nice post! Please do keep us posted when we can see a follow up! design agencies sf

    ReplyDelete
  9. Very nice design and style and fantastic subject matter, very little else we want : D. design agency san francisco

    ReplyDelete
  10. I just added this blog to my feed reader, great stuff. Cannot get enough! web designer san francisco

    ReplyDelete
  11. You should take part in a contest for probably the greatest blogs on the web. I’ll recommend this web site! web designer san francisco

    ReplyDelete
  12. Thank 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

    ReplyDelete
  13. Useful info. Fortunate me I discovered your website accidentally, and I am surprised why this coincidence didn’t happened earlier! I bookmarked it. facebook marketing

    ReplyDelete
  14. those polyurethane inflatable boats are very sturdy and some are kevlar lined too but they are expensive* web designer la

    ReplyDelete
  15. there is great money involve when we talk about personal injury, attorneys make great money out of them too,, la web design

    ReplyDelete
  16. This is a great blog. and i want to visit this every day of the week ,  web designer los angeles

    ReplyDelete
  17. I 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

    ReplyDelete
  18. Admiring 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

    ReplyDelete
  19. Hello! Good stuff, please keep us posted when you post again something like that! website tips

    ReplyDelete
  20. Possible 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

    ReplyDelete
  21. eCommerce Website Designers who are 100% Australian developers from Website Development Australia. We build high performing eCommerce websites. ecommerce

    ReplyDelete
  22. Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this. Swiss web agency

    ReplyDelete
  23. In 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

    ReplyDelete
  24. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. website redesign

    ReplyDelete