Model.AncestorOrSelf() for navigation returns wrong node
Hello,
This is the first time I'm playing with razor and I'm trying to get top node needed for navigation. The problem is I'm using Model.AncestorOrSelf(), but I don't get the expected node.
Here is my tree:
Here is my razor code:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines;
@using System.Linq;
@{
var top = Model.AncestorOrSelf();
<ul class="navigationBar">
@foreach (var c in top.Children.Where("hideInMenu != true"))
{
<li><a href="@c.Url">@c.Name</a></li>
}
</ul>
}
If I run this code when I'm on the "Wat komt er" (path: /aanbod/wat-komt-er) the node in "top" is "Project 1". If I run this code on "Project 1" (path: /) which shows Home via umbracoInternalRedirect the node in "top" is "Template website". Shouldn't this just be "Project 1" since this is the already the highest node/AncestorOrSelf? This way my navigation won't work on the top node... Is there something I'm doing wrong?
I already tried that and it gives the same result. The strange thing is that the "Template website" node isn't even the parent of the "Project 1" website. They are on the same level so why does Model.AncestorOrSelf(1) return that node?
Right, it's doing it the wrong way around then :( It's selecting the first node found on Level 1. I'm afraid I have no solution for that at the moment, you can write your own foreach parent loop and look at the level.
Ok I just discovered that I had a redirect to the wrong node if I opened the home page... That's why it didn't work on the home page and did work on the other pages. Now the correct page is opened on the home page and everything works :).
Model.AncestorOrSelf() for navigation returns wrong node
Hello,
This is the first time I'm playing with razor and I'm trying to get top node needed for navigation. The problem is I'm using Model.AncestorOrSelf(), but I don't get the expected node.
Here is my tree:
Here is my razor code:
If I run this code when I'm on the "Wat komt er" (path: /aanbod/wat-komt-er) the node in "top" is "Project 1". If I run this code on "Project 1" (path: /) which shows Home via umbracoInternalRedirect the node in "top" is "Template website". Shouldn't this just be "Project 1" since this is the already the highest node/AncestorOrSelf? This way my navigation won't work on the top node... Is there something I'm doing wrong?
Jeroen
If you omit where you want AncestorOrSelf to go, it'll go all the way up to level -1 (Content).
Your problem should be solved if you tell it to stop going up higher by specifying the level of "project 1":
I already tried that and it gives the same result. The strange thing is that the "Template website" node isn't even the parent of the "Project 1" website. They are on the same level so why does Model.AncestorOrSelf(1) return that node?
Jeroen
Right, it's doing it the wrong way around then :( It's selecting the first node found on Level 1. I'm afraid I have no solution for that at the moment, you can write your own foreach parent loop and look at the level.
Ok I just discovered that I had a redirect to the wrong node if I opened the home page... That's why it didn't work on the home page and did work on the other pages. Now the correct page is opened on the home page and everything works :).
Jeroen
is working on a reply...