Copied to clipboard

Flag this post as spam?

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


  • Carlos Mosqueda 244 posts 435 karma points
    Jan 20, 2021 @ 01:27
    Carlos Mosqueda
    0

    Annoying problem. Using .Parent.Parent is there a better way?

    So I have a bit of an annoying issue.
    Umbraco 7, Razor and trying to traverse but using 'AncestorsOrSelf' is giving me an IPublishedContent error about it not having "HasValue", and 'AncestorOrSelf' brings back a "False", so I am using .Parent.Parent as an option to do 2 level traversing. Even though I have tried "AncestorsOrSelf(2)" and "AncestorOrSelf(2)". There has to be a better way. Any ideas?

    More or less, I am trying to get the logo of a parent node that is a 'Subsite' of my main home node.

    Tree lays out like this

    Home

    • Page
    • Page
    • Subsite Home
      • Subsite Page
      • Subsite Page

    //=== code is below

    if(Model.Content.Parent.Parent.DocumentTypeAlias == "subsiteHome"){
        if(Model.Content.Parent.Parent.HasValue("subsiteLogo")){
           //Do something
        }
    }
    
  • Steve Morgan 1349 posts 4459 karma points c-trib
    Jan 20, 2021 @ 09:23
    Steve Morgan
    0

    Hi Carlos,

    Are you a little confused here...? I'm assuming the code you've posted is on the Subsite Page.

    Where are you trying to get the subsite Logo from - from the Home node at the top or from the subsite Home?

    For Home it would be Model.Content.AncestorOrSelf(1) for the SubsiteHome it would be Model.Content.AncestorOrSelf(2).

    The reason I ask is from the name of the property subsiteLogo I would expect it to be on the SubsiteHome but Parent.Parent will be serving the Home node here... so you'd expect just a single Parent..

    If you run something like:

    @{
        var home = Model.Content.AncestorOrSelf<Home>(1);
    
        <h2>home = @home.Id @home.Name  </h2>
        <h2>Same as @Model.Content.Parent.Parent.Id @Model.Content.Parent.Parent.Name</h2>
        <img src="@(home.GetPropertyValue<IPublishedContent>("subsiteLogo").Url)" />
    }
    
    @{
        var subsiteHome = Model.Content.AncestorOrSelf<SubsiteHome>(2);
    
        <h2>subsiteHome = @subsiteHome.Id @subsiteHome.Name  </h2>
        <h2>Same as @Model.Content.Parent.Id @Model.Content.Parent.Name</h2>
        <img src="@(subsiteHome.GetPropertyValue<IPublishedContent>("subsiteLogo").Url)" />
    }
    

    You should get the home and then the subsite home with the same node Ids and names...

    The only other thing I can think of is you've bound a host name to the subsite??

Please Sign in or register to post replies

Write your reply to:

Draft