Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Jun 18, 2014 @ 11:09
    Kate
    0

    Find first item in a list with a specific NodeTypeAlias

    Hi

    I have a menu where I need to get the first item that dont use the NodeTypeAlias "OverskriftHolder".

    or said in anothe way. I always need the second item in the menu.

    This is what I got so fare

    @if (Model.Descendants().Any())
    {

    @* Display the current page as the last item in the list *@
    <div id="click" class="clickLink">Sub-menu</div>
    <div id="slideUp" class="navSubpagesBottom">
    @*<h2> Sub-menu for @Model.Name</h2>*@

    <ul>
    @* For each page in the descendants collection which have been ordered by Level (so we start with the highest top node first) *@
    @foreach (var page in Model.Descendants().OrderBy("Level"))
    {
    if(page.NodeTypeAlias != "OverskriftHolder"){

    <li><a href="@page.Url">@page.Name</a> &raquo;</li>
    }


    }


    </ul>
    </div>
    }

    Hope you can help me :-)

    /Kate

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 18, 2014 @ 11:13
    Dennis Aaen
    0

    Hi Kate,

    You can use the .First() method to get the first item of an collection.

    You can find the documentation here: http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Collections#First%28%29

    Hope this helps

    /Dennis

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 18, 2014 @ 11:14
    Jeavon Leopold
    0

    Hi Kate,

    Before answering, can I ask what version of Umbraco you are using and assuming it's newer than v4.10 are you fixed to use a legacy Razor Macro rather than a recommended Partial View Macro? If you don't know what your using, could you please post the @inherits from the top of your cshtml as we can work out from that. All this could all be irrelevant if this is a strongly typed partial view......

    Jeavon

  • Kate 267 posts 610 karma points
    Jun 18, 2014 @ 11:32
    Kate
    0

    Jeavon: It says: @inherits umbraco.MacroEngines.DynamicNodeContext

    Dennis: Dont think I can use .First(), because it is the second item I need

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 18, 2014 @ 11:35
    Dennis Aaen
    1

    Hej Kate,

    Then you can combine it with a .Next(). So your collection would be .First().Next()

    So something like this:

    @foreach(var page in Model.Descendants().First().Next().OrderBy("Level"))

    .Hope this helps,

    /Dennis

  • Kate 267 posts 610 karma points
    Jun 18, 2014 @ 11:40
    Kate
    0

    Hi Dennis

    I got it to work.

    But insted of using @foreach I did it like this.

    @if(Model.Children.Where("Visible").Count() > 0){

    <a href='@Model.Children.First().Next().Url'>@Model.Children.First().Next().Name</a>

    }

    And tha seems to work :-)

    Thanks for your help

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 18, 2014 @ 11:44
    Jeavon Leopold
    0

    Hi Kate,

    Glad you've fixed it. Meanwhile, if you can, you should use a Partial View Macro instead of this legacy Razor Macro. Descendants() is very slow in DynamicNode (which you are using) but is fine in IPublishedContent (Partial View Macro).

    Jeavon

  • Kate 267 posts 610 karma points
    Jun 18, 2014 @ 11:47
    Kate
    0

    Hi Jeavon

    Okay, thanks.

    I guess I have to start read about Partial View Macro ;-)

    /Kate

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 18, 2014 @ 11:52
    Jeavon Leopold
    0

    This code would be identical except you would need to substitute Model with CurrentPage

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies