Copied to clipboard

Flag this post as spam?

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


  • David 29 posts 200 karma points
    Feb 21, 2019 @ 13:18
    David
    0

    Getting Property value from the Homepage and displaying throughout in a partial view

    Afternoon all,

    I am completely new to Umbraco/MVC and I'm working on the following which I wounder if someone could give me a hand with? (using v7.13.2)

    Each page on my site has the ability to change the background image of the header on that page, but I would like to add: If no image has been selected on a page, go to the homepage and use the image from there.

    This is what I have so far:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
    string topBannerImage = Umbraco.Media(imageId).Url;
    
    if (string.IsNullOrWhiteSpace(topBannerImage))
    {
    
        var homePageImage = Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage").Url;
        topBannerImage = homePageImage;
    
    }<header style="background-image:url('@topBannerImage')"></header>
    

    The above works for picking a image per page, but not by going to the root level and getting the value from there if the Media picker is empty.

    I have looked through forums/other sites and had no luck with this.

    Can anyone help?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 21, 2019 @ 22:37
    Alex Skrypnyk
    0

    Hi David

    Use this code:

    int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
    string topBannerImage = imageId >0 ? Umbraco.Media(imageId).Url : string.Empty;
    
    if (string.IsNullOrWhiteSpace(topBannerImage))
    {
    
        var homePageImage = Model.Content.Site().GetPropertyValue<IPublishedContent>("topBannerImage").Url;
    
        topBannerImage = homePageImage;
    }
    

    This method:

    Model.Content.Site().GetPropertyValue
    

    Always looks at the root node, so it's what you need.

    Thanks,

    Alex

  • David 29 posts 200 karma points
    Feb 22, 2019 @ 08:51
    David
    0

    Thanks for this Alex. I have implemented my code as you have suggested, but the page is returning: "Object reference not set to an instance of an object.

    Here is what I have in my partial view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
    string topBannerImage = imageId > 0 ? Umbraco.Media(imageId).Url : string.Empty;
    
    
    if (string.IsNullOrWhiteSpace(topBannerImage))
    {
    
        var homePageImage = Model.Content.Site().GetPropertyValue<IPublishedContent>("topBannerImage").Url;
        topBannerImage = homePageImage;
    
    }
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 22, 2019 @ 21:21
    Alex Skrypnyk
    0

    Hi David,

    In which line exception?

    Can you change :

    string topBannerImage = imageId > 0 ? Umbraco.TypedMedia(imageId).Url : string.Empty;
    

    Umbraco.TypedMedia instead of Umbraco.Media

    Thanks,

    Alex

  • David 29 posts 200 karma points
    Feb 25, 2019 @ 09:28
    David
    0

    Alex,

    This is the line in question it is complaining about:

    var homePageImage = Model.Content.Site().GetPropertyValue<IPublishedContent>("topBannerImage").Url;
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 25, 2019 @ 16:12
    Alex Skrypnyk
    0

    Hi David

    Please, check what do you have in the root node in "topBannerImage" property.

    There is should be a media item selected.

    Thanks,

    Alex

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 25, 2019 @ 16:13
    Alex Skrypnyk
    0

    To avoid this exception you can use this code:

        var homePageImage = Model.Content.Site().GetPropertyValue<IPublishedContent>("topBannerImage");
        if (homePageImage != null)
        {
            topBannerImage = homePageImage.Url;
        }
    
  • David 29 posts 200 karma points
    Feb 25, 2019 @ 16:26
    David
    100

    Thanks for the help on this Alex. Earlier today I managed to get this working using the following:

    var homePageImage = Umbraco.AssignedContentItem.Ancestor(1).GetPropertyValue<IPublishedContent>("topBannerImage").Url;
    
  • 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