Copied to clipboard

Flag this post as spam?

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


  • BEWD 89 posts 301 karma points
    Dec 14, 2015 @ 21:39
    BEWD
    0

    If Else statement

    Hi All

    Sorry for this very basic question but I have searched around and cannot seem to find an example that works for me.

    I have a website where I want the client to be able to add an image of any staff they may have, however, if they do not have one I would like it to by default add a placeholder image rather than none at all.

    I have figured out how to ignore a blank image and display nothing which I have as

    @if(CurrentPage.HasValue("centerImage"))
        {
        <img src="@Umbraco.Media(CurrentPage.centerImage).Url" />
        }
    

    But to get it to default to an image if one isn't selected I struggle with. I have tried the code below but it errors with

    "The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])' "

    @if(CurrentPage.HasValue("aboutLeftImage"))
        {
        <img src="@Umbraco.Media(CurrentPage.aboutLeftImage).Url" />
        }
    else
        {<img src="images/default.png" />
        }
    

    Any suggestions or pointers would be gratefully received.

    Thanks

    Ben

  • gary 385 posts 916 karma points
    Dec 14, 2015 @ 23:27
    gary
    0

    Hi BEWD

    If your default image will always be the same, ie nobody will want to change it, would suggest create a div and serve it as a background image via css.

    If someone may want to change it at some point, put it in Umbraco on your "site" tab and call it from there as per the "aboutLeftImage".

    Reason it's not working is that aboutLeftImage is in the Umbraco environment and images/default.png is in the root of the site, so without direction to get there, it cannot find it.

    Hope that makes sense and helps you move forward.

    Please ask again if you need anything more.

    Regards

    Gary

  • Tim Miller 32 posts 252 karma points
    Dec 14, 2015 @ 23:45
    Tim Miller
    100

    If the image path was just wrong he wouldn't be getting a .NET error. What gary is saying is right though, you will probably want your image path to have a leading /.

    <img src="/images/default.png" />
    

    The error looks like a datatype mismatch. Umbraco.Media() expects an int. You need to convert your value to an int. The easiest way would probably be to just cast it like this:

    <img src="@Umbraco.Media(Model.Content.GetPropertyValue<int>("aboutLeftImage")).Url" />
    
  • BEWD 89 posts 301 karma points
    Dec 15, 2015 @ 17:31
    BEWD
    0

    Thanks guys, I cant believe it was all down to a missing /

    Just adding that in has fixed the issue, I cant believe I missed that.

    Thanks for your help and apologies for wasting your time :)

Please Sign in or register to post replies

Write your reply to:

Draft