Copied to clipboard

Flag this post as spam?

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


  • Dibs 202 posts 991 karma points
    Feb 01, 2017 @ 14:14
    Dibs
    0

    Traversing - get next node

    Dear Umbraco Team

    I have a node with a few child nodes. i would like to render content from a node between the first and last node. i am able to get content from both first and last child nodes using the following code

     @{ var firstChild = @Model.Content.Children.First();}
     @{ var lastChild = @Model.Content.Children.Last();}
    

    But when i try the following code i get YSOD

     @{ var nthChild = @Model.Content.Children.Next(1);}
    

    Am i missing some thing ? Any assistance would be great

    Thanks Dibs

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Feb 01, 2017 @ 14:25
    Paul Wright (suedeapple)
    0

    You can use the Siblings() extension.

    Or you could use FollowingSibling which has a few overloads.

    @Model.Content.FollowingSibling("textPage",true)

    The gets the next sibling, and if you're on the last one, it'll wrap round to the first.

    Or something like this...

    @{
    var nodes = Model.Content.Siblings().OrderBy( x => x.SortOrder);
    }
    
    @foreach (var n in nodes)
        {
    <p> n.Name</p>
    
     }
    
  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Feb 01, 2017 @ 14:32
    Dennis Adolfi
    0

    Hi Dibs.

    So the Next() method is an extension method for a singe IPublishedContent model, and it returns the node next to the current model in the tree.

    Your are trying to use it on a List of IPublishedContent, and that does not work.

    Instead if you browse a child node and have this in your view: @Model.Content.Next(), that would return the next child after your current browsing child.

    Make sure however to null check, because I think the .Next() method throws a null reference exception if the child your browsing does not have a next child or is the only child for instance.

    Hopefully this makes sense.

    Also have a look at the Querying/Traversing documentation, that might help you: https://our.umbraco.org/documentation/reference/templating/mvc/querying

    Best of luck!

  • Dibs 202 posts 991 karma points
    Feb 01, 2017 @ 14:46
    Dibs
    101

    Cheers Paul & Dennis

    While you were giving me feedback, I got the following to work.

    @{ var child = @Model.Content.Children.First();}

    @child.Next(nth).GetPropertyValue("property")

    Thanks Dibs

Please Sign in or register to post replies

Write your reply to:

Draft