Copied to clipboard

Flag this post as spam?

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


  • Mihir Ajmera 32 posts 145 karma points
    Aug 02, 2019 @ 05:05
    Mihir Ajmera
    0

    Access Parent Node property in Child Template View MVC Razor in Umbraco 8.1.0

    Hello All,

    I have one articles list (parent node) and article detail (child node) document type. In article list document type I have taken Header section background image, HeaderText and SEO modules property. In article detail screen I have numbers of column like Article Name, Article date, Article Image, description property etc.

    As per above I nested the article detail with article list and using query builder I render article list in front-end with order by descending created date. Please refer below query.

    var section = Umbraco.Content(Guid.Parse("7c4e163e-cd41-42c1-af3e-26bfd0530d7c")) .ChildrenOfType("createArticle") .Where(x => x.IsVisible()) .OrderByDescending(x => x.CreateDate)

    But my requirement is I need display article list based on article date which property available in article detail page. so problem with query builder I can`t access children property in where condition and set the list in order by descending with article date property. So any one know how to access children property in MVC RAZOR of Umbraco 8.1.0 version.

    Second problem is I can`t access parent Header Background Image in child template view just like

    So if you have solution about this can you please help me so I can work with common component without add every time new property child node.

    Thanks in Advance.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 03, 2019 @ 19:21
    Marc Goodson
    100

    Hi Mihir

    Would I be write in thinking you are after something like this?

    var section = Umbraco.Content(Guid.Parse("7c4e163e-cd41-42c1-af3e-26bfd0530d7c")) .ChildrenOfType("createArticle") .Where(x => x.IsVisible()) .OrderByDescending(x => x.HasValue("articleDate") ? x.Value<DateTime>("articleDate") : x.CreateDate);
    

    and in your article template page, if you want to read the property from the article list document type, you can use 'Fallback.ToAncestors' which will recurse up the content tree until it finds a property of that alias that is populated:

    @{
    var newsListBackgroundImage = Model.Value<IPublishedContent>("backgroundImage", fallback: Fallback.ToAncestors)
    }
    

    regards

    Marc

  • Mihir Ajmera 32 posts 145 karma points
    Aug 05, 2019 @ 04:39
    Mihir Ajmera
    0

    Thanks Marc Goodson,

    Its work both solutions I can easily access parent - child property in list and access parent image in child view.

    Again Thank you so much for quick response.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 05, 2019 @ 08:06
    Marc Goodson
    0

    Great Mihir! - glad that you have gotten it working!

Please Sign in or register to post replies

Write your reply to:

Draft