Where developers come to connect, share, build and be inspired.

10

Bootstrap & MVC - Select active link on 'Pills Navbar'

675 views

I used this approach to select the right 'pill' in the navigation:

<li @if (@ViewContext.RouteData.GetRequiredString("controller") == "Home") { @Html.AttributeEncode("class=active"); }><a href="/">Home</a></li>

If you call the GetRequiredString() method with the parameter "controller", it returns the current controller-name as string.

It seems a little hacky, but it's faster than to write an extension method or do the selection manually with javascript.

Comments

  • 0d5c14a2404263dee61489705318fb69_normal
    bossoj

    Nice! Спасибо ))

  • Blank-mugshot
    gordsh

    Wow, spent over 3 hours combing the web trying different suggestions and this one rocks. IT is sooo simple I don't know how you came up with it. Thanks a million.

Add a comment