Copied to clipboard

Flag this post as spam?

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


  • Ian Smedley 97 posts 192 karma points
    Feb 26, 2013 @ 18:11
    Ian Smedley
    0

    V6 - MVC Razor Where has AncestorOrSelf Gone

    This code is from a Razor Macro on a previous version of Umbraco - it works.

    In Umbraco 6 there's no such thing as AncestorsOrSelf (or so VS tells me)

     

     var currentPage = @Model;
    
        //Will walk up the tree to the last ancestor node aka RootNode
        var rootNode = currentPage.AncestorsOrSelf()[0];

    So - where has it gone wrong - I can find loads of documentation and reference to it, I've tried some variations, but I can't seem to find it.

    I've got this now in a "MacroPartials" and it's supposed to be a simple Navigation script.

    Where's it gone?

  • gary 385 posts 916 karma points
    Feb 26, 2013 @ 19:05
    gary
    0

    Hi Ian

    Yes, but not yet, was left private, use AncestorOrSelf, should give you what you require for now.

    Hope it helps G

  • Ian Smedley 97 posts 192 karma points
    Feb 27, 2013 @ 14:48
    Ian Smedley
    0

    I've just written a function which loops through the parent until it's at the top - does the same job.....

        IPublishedContent getRootNode(IPublishedContent node)
        {
            while (node.Parent != null)
            {
                node = node.Parent;
            }
            return node;
        }
  • Mykola 1 post 22 karma points
    May 27, 2013 @ 15:48
    Mykola
    1

    You can try this solution:

    @inherits umbraco.MacroEngines.DynamicNodeContext

     

    @using System.Linq

    @{  

        var rootItem = this.Current.AncestorOrSelf(1);

        var menuItems = this.Current.AncestorOrSelf(1).Children.Where(p => p.HasProperty("umbracoNaviHide") &&

            (string)p.GetProperty("umbracoNaviHide").Value != "1").ToList();

        menuItems.Insert(0, rootItem);

        

    }

    <div id="midt_local">

        <div id="meny_top_local">

            <div id="knappe-container_local">

                <ul id="knappe-menu_local">

                    @foreach (umbraco.MacroEngines.DynamicNode item in menuItems)

                    {

                        bool currentPage = item.Id == this.Current.Id;

                        <li>

                            <a href="@item.Url" @Html.Raw(currentPage ? "class=\"current\"" : string.Empty)>

                                @item.GetProperty("umbnavigationName").Value

                            </a>

                        </li>       

                    }

                </ul>

            </div>

        </div>

    </div>

     

     

  • Proxicode 128 posts 324 karma points
    Jan 21, 2014 @ 02:32
    Proxicode
    0

    Thanks @Ian Smedley! I was able to fit your code into a perfect helper function for me to get the parent "section" of each of me pages while stopping before crawling up to the homepage. Exactly what I needed thanks again!

    @helper getRootNode(IPublishedContent node) { 
         while (node.Parent.Id != 1115) { 
              node = node.Parent; }
    <h1>@node.Name</h1>
    

    Then in my markup I can just use

    <div class="headline container">
        <div class="row">
            <div class="page-title twelvecol">
                @getRootNode(CurrentPage)
            </div>      
        </div>
    </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft