Copied to clipboard

Flag this post as spam?

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


  • Aaron 5 posts 83 karma points
    Oct 28, 2016 @ 14:02
    Aaron
    0

    Access last child in children

    I've created a condition that checks to see if the current child node is the first in the set of children. If it isn't the first child then the URL is generated easily with .Previous().Url. If it is the first child it needs to default to the last child in the set of children.

    @if( @CurrentPage.Previous() == null ) {
        prevUrl = @CurrentPage.Up().LastChild().Url;
    } else {
        prevUrl = @CurrentPage.Previous().Url;
    }
    

    As you can see above I added .LastChild() after @CurrentPage.Up() knowing that this doesn't work but I thought it would illustrate the desired functionality. I've searched all over for the solution but haven't had any luck yet. Thanks for any help!

  • Alex Brown 129 posts 620 karma points
    Oct 29, 2016 @ 18:15
    Alex Brown
    0

    Would System.Linq.Last() method not work here?

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Oct 31, 2016 @ 08:54
    Dennis Adolfi
    101

    As Alex mentioned, you should be able to use LINQ .Last() in this case. Here is an example of how this could be solved with a little helper:

    @helper RenderPrev(IPublishedContent model)
    {
        if (model.Parent != null)
        {
            var prevUrl = model.Previous() == null ? model.Parent.Children.Last().Url : Model.Content.Previous().Url;
                <a href="@prevUrl">Previous</a>
        }
    }
    
    @RenderPrev(Model.Content)
    
  • Aaron 5 posts 83 karma points
    Nov 04, 2016 @ 21:58
    Aaron
    1

    That worked exactly as needed. Thanks!

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Nov 05, 2016 @ 09:22
    Dennis Adolfi
    0

    Awesome! I'm glad I could help and thanks for the response!

    Have a great weekend, take care!

Please Sign in or register to post replies

Write your reply to:

Draft