Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Nov 27, 2015 @ 21:16
    Paul Griffiths
    0

    Accessing a same level node in Razor

    Hi All,

    I have the following structure in my back office and when on the home node I would like to access the Carousel Repository node and display its contents.

    --Home (Level 1)

    ----Node (level 2)

    ----Node (level 2)

    --Carousel Repository (Level 1)

    I am able to do it using

    @{ var CarouselRepository = Umbraco.Content(1063);  }
    

    But i would like to get it by docTypeAlias rather than use an ID

    Can someone please help im having a dull moment :)

    Many thanks

    Paul

  • gary 385 posts 916 karma points
    Nov 27, 2015 @ 21:33
    gary
    0

    Hi Paul

    Had that same dull moment earlier - it wasn't as obvious as I thought.

    @{ var q = @CurrentPage.Parent().Descendants("Work").First();}
    

    Work is my docType alias, then just q.Title to get property of "Title".

    Work is actually on the 2nd Level, hence the .First() to get first child.

    Anyhow, sure you can see what you need from here.

    Cheers

    Gary

  • Paul Griffiths 370 posts 1021 karma points
    Nov 27, 2015 @ 22:11
    Paul Griffiths
    0

    Hi Gary,

    Thanks for the reply mate. Im still getting an error with that code.

    enter image description here

    I think i am getting this error because i am on the home page and the parent is null i just need to get the carouselRepository page which is at the same level.

    enter image description here

    FYI i am using a partial view on the homepage template.

    Think im getting tired ;)

    Cheers

    Paul

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Nov 27, 2015 @ 23:11
    Nicholas Westby
    1

    Try this:

    var carousel = Model.Content.Siblings().FirstOrDefault(x => "CarouselRepository".InvariantEquals(x.DocumentTypeAlias));
    

    I always prefer Model.Content over CurrentPage, as the latter is dynamic and you don't get intellisense.

    By the way, you also don't need to add an at sign (@) before an expression or variable unless you intend to output that variable or expression.

  • gary 385 posts 916 karma points
    Nov 27, 2015 @ 23:16
    gary
    0

    Hi All

    Sorry missed your reply.

    Problem is - - you have no top node, all are at the same level, therefore you have no descendants or siblings.

    Top Node (domain) ----Home ----Carousel Repository

    Will give you parent (Top Node) and Home and Carousel as descendants / children.

    Tiredness gets to us all.

    Cheers

    Gary

Please Sign in or register to post replies

Write your reply to:

Draft