Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 21, 2011 @ 18:22
    Jeroen Breuer
    0

    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?

    Jeroen

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 22, 2011 @ 09:18
    Sebastiaan Janssen
    0

    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":

    var top = Model.AncestorOrSelf(1);
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 22, 2011 @ 14:51
    Jeroen Breuer
    0

    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 

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 22, 2011 @ 14:56
    Sebastiaan Janssen
    0

    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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 26, 2011 @ 10:19
    Jeroen Breuer
    1

    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

Please Sign in or register to post replies

Write your reply to:

Draft