Copied to clipboard

Flag this post as spam?

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


  • Stefan Joseph 19 posts 120 karma points
    Aug 04, 2017 @ 15:24
    Stefan Joseph
    0

    Setting an inline background image with the Media Picker

    Hoping for a quick and easy solution to this - I have a media picker set up that would allow users to select a background image.

    However, the following code set up in my partial view:

    <div class="banner text-center" style="background-image:url('@Umbraco.Field("contactBannerBackgroundImage")');">
    

    Is outputting as:

    <div class="banner text-center" style="background-image:url('1223');">
    

    Any help appreciated!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 04, 2017 @ 22:11
    Alex Skrypnyk
    100

    Hi Stefan

    Welcome to our forum!

    Try this code:

    <div class="banner text-center" style="background-image:url('@Umbraco.TypedMedia(Umbraco.AssignedContentItem.GetPropertyValue<int>("contactBannerBackgroundImage")).Url');"></div>
    

    "contactBannerBackgroundImage" property stores id of the media item, so you have to get Media item by id with Umbraco.TypedMedia(id) method and after that retrieve URL of the image.

    Thanks,

    Alex

  • Stefan Joseph 19 posts 120 karma points
    Aug 06, 2017 @ 21:14
    Stefan Joseph
    1

    Thanks for your help Alex, that worked perfectly!

    Front end development is more my thing so I really appreciate the explanation.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 06, 2017 @ 21:17
    Alex Skrypnyk
    0

    You are welcome, Stefan

    Have a great time with Umbraco :)

    Alex

  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 08:51
    Stefan Joseph
    0

    Hi Alex, I wonder if you could help me a little further with this - I've had success with this code via templates - but it doesn't seem to work in Macro Partial Views.. any ideas?

    Error Screen when attempting to use code with Macro Partial Views

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 08:56
    Alex Skrypnyk
    0

    Hi Stefan,

    Can you show all macro code?

    Thanks,

    Alex

  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 09:21
    Stefan Joseph
    0

    Sure no problem:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <section class="banner top-banner" style="background-image:url('@Umbraco.TypedMedia(Umbraco.AssignedContentItem.GetPropertyValue<int>("pageBannerBackgroundImage")).Url');">
        <div class="container">
            <h1>@Model.MacroParameters["pageTitle"]</h1>
            <div class="zip-divider"></div>
        </div>
    </section>
    

    I presume there's a slight variation in the calling method for Macros?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 09:27
    Alex Skrypnyk
    0

    Stefan, please check that current page has value:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @if (Umbraco.AssignedContentItem.HasValue("pageBannerBackgroundImage"))
    {
        <section class="banner top-banner" style="background-image: url('@Umbraco.TypedMedia(Umbraco.AssignedContentItem.GetPropertyValue<int>("pageBannerBackgroundImage")).Url');">
            <div class="container">
                <h1>@Model.MacroParameters["pageTitle"]</h1>
                <div class="zip-divider"></div>
            </div>
        </section>
    }
    
  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 09:34
    Stefan Joseph
    0

    No error page, but the macro doesn't actually display on the page now.

  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 09:35
    Stefan Joseph
    0

    No error page, but the macro is no longer displaying on the page either.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 09:35
    Alex Skrypnyk
    0

    So the problem is that current page doesn't have "pageBannerBackgroundImage" property value.

  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 09:40
    Stefan Joseph
    0

    But I have defined a title and image to be used in the 'content' tab - everything is set up correctly in the macro as well.. so I don't understand why the page doesn't have the pageBannerBackgroundImage value?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 09:44
    Alex Skrypnyk
    0

    In the macro?

    Where did you define image?

    This method works with Current Umbraco Page, not the macro settings.

    Umbraco.AssignedContentItem.GetPropertyValue("pageBannerBackgroundImage")
    
  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 09:52
    Stefan Joseph
    0

    so, in the macro I have:

    pageBannerBackgroundImage as an Alias for a media picker

    In the macro partial view:

    the code I pasted in (along with your amendments)

    With the idea being, that via the media picker I can upload an image, and it will display as the background image

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 09:58
    Alex Skrypnyk
    1

    Sorry, I thought you have "pageBannerBackgroundImage" property on the page, you can use this code in this situation:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @if (Model.MacroParameters["pageBannerBackgroundImage"] != null)
    {
        var pageBannerBackgroundImage = Model.MacroParameters["pageBannerBackgroundImage"];
    
        <section class="banner top-banner" style="background-image: url('@Umbraco.TypedMedia(pageBannerBackgroundImage).Url');">
            <div class="container">
                <h1>@Model.MacroParameters["pageTitle"]</h1>
                <div class="zip-divider"></div>
            </div>
        </section>
    }
    
  • Stefan Joseph 19 posts 120 karma points
    Sep 04, 2017 @ 10:02
    Stefan Joseph
    0

    My man!! Thank you so much Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 04, 2017 @ 10:02
    Alex Skrypnyk
    0

    You are welcome, have a nice day!

Please Sign in or register to post replies

Write your reply to:

Draft