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 3 posts 73 karma points
    6 days ago
    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 32 posts 113 karma points
    6 days ago
    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 3 posts 73 karma points
    6 days ago
    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 32 posts 113 karma points
    6 days ago
    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 330 posts 1536 karma points
    6 days ago
    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 3 posts 73 karma points
    6 days ago
    Peter van Geffen
    0

    That's it. That works for me!

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft