Copied to clipboard

Flag this post as spam?

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


  • Peter van Geffen 54 posts 290 karma points
    Oct 09, 2019 @ 13:04
    Peter van Geffen
    0

    Retrieve an image from a parent

    Hello everyone,

    I'm pretty new with Umbraco and now i've got the following question;

    People can set a header/hero image, if they don't i got a fallback (code below). But if it's a sublevel i want it to take a look at his parent first and show that image (if it's available) before it shows the fallback. How can i do this?

    @if (image != null)
    {
            <img src="@Url.GetCropUrl(image, "Hero")" alt="@image.Name" />
    }
    else
    {
            <img src="/images/fallback.jpg" alt="" />
    }
    
  • bronzewind 42 posts 125 karma points
    Oct 09, 2019 @ 13:40
    bronzewind
    0

    Hi Peter,

    I'm not sure if you're using strong typed models, but if you don't and your view looks like:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    in the top, then you just do @Model.Parent. Are you using a code editor or are you just using the Umbraco Backoffice?

    If you use Visual Studio, even with the website running you can just help yourself with Intellisense.

    Happy coding!

  • Peter van Geffen 54 posts 290 karma points
    Oct 09, 2019 @ 13:49
    Peter van Geffen
    0

    Hi,

    I'm using the backoffice. My complete code looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{ 
        var image = Model.Content.GetPropertyValue<IPublishedContent>("heroImage");
    }
    <div class="slide-image bg-image-filled">
        @if (image != null)
        {
            <img src="@Url.GetCropUrl(image, "Hero")" alt="@image.Name" class="img-fluid" />
        }
        else
        {
            <img src="/images/fallback.jpg" alt="" class="img-fluid" />
        }
    </div>
    
  • bronzewind 42 posts 125 karma points
    Oct 09, 2019 @ 14:28
    bronzewind
    0

    Something like this you could try:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var image = Model.Content.GetPropertyValue

    @if (image != null) { var parent_image = Model.Content.GetPropertyValue

    Please remember ParentHero should be replaced with the alias you used for the image property in the parent. I highly recommend you to use a code editor so that you can see semantic errors before previewing your changes.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Oct 09, 2019 @ 14:43
    Shaishav Karnani from digitallymedia.com
    0

    Hi Peter,

    If heroImage property is same on the parent page then you can use recursive option in GetPropertyValue. I have added below true.

        var image = Model.Content.GetPropertyValue<IPublishedContent>("heroImage", true);
    

    Below is your code.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{ 
        var image = Model.Content.GetPropertyValue<IPublishedContent>("heroImage", true);
    }
    <div class="slide-image bg-image-filled">
        @if (image != null)
        {
            <img src="@Url.GetCropUrl(image, "Hero")" alt="@image.Name" class="img-fluid" />
        }
        else
        {
            <img src="/images/fallback.jpg" alt="" class="img-fluid" />
        }
    </div>
    

    Hope it helps to solve your issue.

    Cheers,

    Shaishav

  • Peter van Geffen 54 posts 290 karma points
    Oct 09, 2019 @ 14:47
    Peter van Geffen
    0

    That's it. That works for me!

    Thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies