Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Apr 05, 2020 @ 18:03
    Matt
    0

    Using Media picker in another template

    Hi all,

    My mind has gone blank can someone point me in right direction.

    If I have a media picker in doc type A how can I render the image in Doc Types B template?

    I use the following code;

    @{
        var typedMediaPickerSingle = Model.Value<IPublishedContent>("featuredBanner");
        if (typedMediaPickerSingle != null)
        {
            <p>@typedMediaPickerSingle.Url</p>
            <img src="@typedMediaPickerSingle.Url" style="width:200px" alt="@typedMediaPickerSingle.Value("alt")" />
        }
    }
    

    Which works fine if I wanted to use in in Doc Type A template, but not doc type B template?

    Thanks

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Apr 06, 2020 @ 19:13
    Marc Goodson
    0

    Hi Matt

    It would depend on how Document Type B content relates to Document Type A content in your Umbraco Content Tree.

    If Document Type B is a child of Document Type A, then you could read the property from the 'Parent' property in Document Type B's template

    eg

    var typedMediaPickerSingle = Model.Parent.Value<IPublishedContent>("featuredBanner");
    

    if Document Type B is a descendant of Document Type A, then you could use a recursive fallback to read the property:

    var typedMediaPickerSingle = Model.Value<IPublishedContent>("featuredBanner", fallback: Fallback.ToAncestors);
    

    If Document Type B is not in anyway consistently related to Document Type A in the Content Tree, then it will be a case of 'traversing' to that particular node, and retrieving it as IPublishedContent to be able to read it's properties eg. perhaps starting at the root of the site, perhaps there is only one page of DocType A... then something like this might work:

    var docTypeA = Umbraco.ContentAtRoot().Descendants().FirstOrDefault(f=>f.ContentType.Alias == "documentTypeA"));
        var typedMediaPickerSingle = docTypeA.Value<IPublishedContent>("featuredBanner");
    

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft