So, as @Seb mentioned this is the best method as no empty class attribute (through some magic), mini correction on @Sebs post above as Model should be Model.Content
Yeah, I think it's because @Umbraco.If returns HtmlString instead of bool. Not having the empty attributes is a cool Razor feature to know about though!
To clarify one thing, this will add class="active" to only the level 2 node that is a child of the root node.
For instance on the txt starter kit, when viewing a level 3 node, a news article only the level 2 node, "news" has the class="active" not the article itself.
While it could do with an update (it wrote it when MVC was first introduced in v4.10), you might find this navigation code I wrote interesting. There are some instructions on how to use it here.
Wow Jeavon, that looks crazy :D But you my problem is that I don't understand more than 10% of that script - and it's very important for to understand what I'm doing :D
If CurrentPage add active class
Hi Guys
I'm trying to make my first Razor navigation.
My problem right now is I really don't know how to add an class to my active node.
Can anyone help me with this - and maybe try to make me understand?
Hi Mads
This should get you working
i found out that if i was using bootstrap 4 i didt not work but if i used bootstra 3 it works i dont no if it help you but it workt for me
https://our.umbraco.com/forum/templates-partial-views-and-macros/96035-how-do-i-add-the-bootstrap-css-class-active-to-the-menu-when-clicked
Sorry you want the span tag to display right
Hi Fuji
Sorry the Span was just for testing :D
But the first one
Gives me an error: CS0103: The name 'Library' does not exist in the current context
And can you maybe explan what you are doing?
Intriguing approach Fuji!
This would be equivalent in MVC
@Umbraco.If(mainNodes.IsAncestorOrSelf(Model.Content),"current", " ")
And even better:
This will produce either
<li class="current"><a href="">Name</a></li>
or<li><a href="">Name</a></li>
. No more empty class attributes!This will probably work as well but I haven't tested:
Guess I didn't ready very well, I see the ternary operator and the null trick is used in the opening post already. My bad, carry on! ;)
@Seb, Also, I don't think your second one will work,
@Library
doesn't exist in Mvc :-)@Jeavon I saw.. :) Updated to use your sample instead!
Ah really need to get my hands on mvc. Damn it.
Sorry guys
This one does leave a empty class attribute
So, as @Seb mentioned this is the best method as no empty class attribute (through some magic), mini correction on @Sebs post above as
Model
should beModel.Content
@Jeavon Guess it only works with "Native" MVC then! Thanks for the test, good to know! :)
Yeah, I think it's because
@Umbraco.If
returns HtmlString instead of bool. Not having the empty attributes is a cool Razor feature to know about though!Hi guys
Thanks for all your posts.
But I'm really sorry to said that none of your solutions didn't work
This line just throws me an empty <li> no class added????
Hi Mads,
Strange, this is the "best" solution:
Jeavon
I've made a few further optimizations and tested with the v7 txt start kit and it seems to work fine:
Hi Jeavon
Thanks for your hugh work, but i'm still sorry to tell you that it just still gives me an <li> and nothing else.
I'm really new to this Razor language.
the script is in an Partial view and i got this part in the top of the fil
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
Maybe i'm doing something wrong?
Hi Mads, it's all fine.
To clarify one thing, this will add class="active" to only the level 2 node that is a child of the root node.
For instance on the txt starter kit, when viewing a level 3 node, a news article only the level 2 node, "news" has the class="active" not the article itself.
Does that make sense?
Jeavon
Jeavon, Jeavon I'm such an idiot :D
My html output looked like this:
So no subpages and the class is only added to <li>'s with subpages :D
So my code now looks like this:
and everything works fine
Awesome!
While it could do with an update (it wrote it when MVC was first introduced in v4.10), you might find this navigation code I wrote interesting. There are some instructions on how to use it here.
Great, thanks a lot :D
- I'll look at it
Wow Jeavon, that looks crazy :D
But you my problem is that I don't understand more than 10% of that script - and it's very important for to understand what I'm doing :D
Absolutely understand, learning is the most fun part!
is working on a reply...