Copied to clipboard

Flag this post as spam?

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


  • Marc 50 posts 182 karma points
    Nov 28, 2020 @ 19:41
    Marc
    0

    Get ancestor field value

    Hello guys !

    I'm trying to get a filed from a parent with the ancestor method but it's not working.

    My tree is like :

    Master > Page 1 > Page 2

    I want my page 2 to get a textbox value from the page 1.

    I really want to use the ancestor method.

    Can you help a bit ? thank you :)

  • Marc 50 posts 182 karma points
    Nov 29, 2020 @ 14:21
    Marc
    0

    up ! :)

  • Huw Reddick 1734 posts 6073 karma points MVP c-trib
    Nov 29, 2020 @ 15:33
    Huw Reddick
    0

    What have you tried, and what happens?

    Ancestors should give you a collection, so in your example

    IPublishedContent page1 = Model.Ancestors().First()

    should give you Page 1 if you are on Page 2

  • Marc 50 posts 182 karma points
    Nov 29, 2020 @ 17:28
    Marc
    0

    hello !

    I tried that :

    @{
                    var parent = Model.AncestorOrSelf(1);
                }
                <div style="background:@parent.("eventsListingGradient");" class="bannerContent">
                        @{
                            var typedMediaPickerSingle = Model.Value<IPublishedContent>("eventListingPageBanner", fallback: Fallback.ToAncestors);
                            if (typedMediaPickerSingle != null)
                            {
                                <img src="@typedMediaPickerSingle.Url" alt="@typedMediaPickerSingle.Value("alt")" />
                            }
                        }
    

    but in the html it shows that :

    <div style="background:Umbraco.Web.PublishedModels.EvenementsListing.(" eventslistinggradient");"="" class="bannerContent">
                                ...
                </div>
    

    Unfortunatly, that's not the value of the field i'm getting.

  • Huw Reddick 1734 posts 6073 karma points MVP c-trib
    Nov 29, 2020 @ 17:55
    Huw Reddick
    0

    I'm not familiar with Fallback, which is what you are actually having an issue with rather than Ancestors.

    This is generally why it helps to post code first if you are having a problem, and explain what exactly you are trying to achieve.

    From your example it would seem that none of the pages in your ancestry have a value for eventListingPageBanner

  • Marc 50 posts 182 karma points
    Nov 29, 2020 @ 18:45
    Marc
    0

    The issue to me is that part <div style="background:@parent.("eventsListingGradient");" class="bannerContent">

    This background:@parent.("eventsListingGradient") is supposed to give me rgb code to change the background color but instead I have that in the html : background:Umbraco.Web.PublishedModels.EvenementsListing.(" eventslistinggradient")

    What i want is in the doc type of page 1, the user enter rgb code and it renders on the page. To avoid the user to enter the rbg code many times, I want page 2 to get the value on page 1.

    I hope it's clear ahah

  • Huw Reddick 1734 posts 6073 karma points MVP c-trib
    Nov 29, 2020 @ 21:21
    Huw Reddick
    100

    try either @parent.Value<string>("eventsListingGradient") or the same as you have done for the image src Model.Value<string>("eventsListingGradient", fallback: Fallback.ToAncestors)

  • Marc 50 posts 182 karma points
    Nov 29, 2020 @ 23:11
    Marc
    0

    Thanks a lot, that was what I was missing !

    Thank you sir :)

  • Huw Reddick 1734 posts 6073 karma points MVP c-trib
    Nov 30, 2020 @ 09:19
    Huw Reddick
    0

    Glad you got it working, could you mark as answered.

Please Sign in or register to post replies

Write your reply to:

Draft