hwtkng
Last Updated: July 20, 2016
·
1.683K
· therealhappy
86d7ef9f160ae20070327f1d45b927a6

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

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.

3 Responses
Add your response

9979

Nice!
Спасибо ))

over 1 year ago ·
11419

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.

over 1 year ago ·
27878

Hi happy, hab leider keine andere Kontaktmöglichkeit gefunden als hier. Ich suche dringend einen richtig guten C# Entwickler wie dich! Entschuldige den Spam! Bela

6 months ago ·
Awesome Job

04cc7f06 d9d8 11e6 9056 9067bcd9fbef
Software Engineer
·
Remote within the United States
·
Full Time