Copied to clipboard

Flag this post as spam?

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


  • Tom Maton 387 posts 660 karma points
    Apr 27, 2012 @ 16:42
    Tom Maton
    0

    if statement to check if property exists and has a value

    Hi All,

    This is my first site using Razor on V5 so relatively new to it all but reading through I thought the following would of worked

    @if(CurrentPage.GetProperty("image").Value != null) {       
            var imageUrl = @Umbraco.GetMediaUrl(CurrentPage.image);        
            <img src="/ImageGen.ashx?image=@imageUrl&compression=25&constrain=false" alt="" class="lowRes" />
            <img src="/ImageGen.ashx?image=@imageUrl&compression=85&constrain=false" alt="" class="highRes"/>
            }

    The idea is to ensure that there is a value in there before getting the media url. But it seems no matter waht its always getting through and failing/erroring because no image is assigned to node.

    Any help greatly appreciated.

    Thanks,
    Tom

  • Tom Maton 387 posts 660 karma points
    Apr 28, 2012 @ 20:49
    Tom Maton
    0

    Bump... This is driving me around twist.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Apr 28, 2012 @ 21:14
    Douglas Robar
    1

    Try this...

    @if (CurrentPage.Image != HiveId.Empty.ToString())

    Alternatively, this might (but might not in all cases) work... @if (!string.IsNullOrEmpty(CurrentPage.Image))

     

    (and throw away the razor cheatsheet from v4 when using v5; the implementations are different. hopefully someone will whip up an equivelent cheatsheet for v5 soon)

    cheers,
    doug. 

  • Tom Maton 387 posts 660 karma points
    Apr 28, 2012 @ 21:42
    Tom Maton
    0

    Doug, I owe you a pint mate. That worked a treat.

    I guess using the v4 cheatsheet doesn't help too :)

    Thanks again,
    Tom 

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Apr 28, 2012 @ 21:49
    Douglas Robar
    0

    You're welcome, Tom!

    See you at CG12 in just over a month!

    d.

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Apr 28, 2012 @ 21:56
    Douglas Robar
    0

    BTW, you'll find this references helpful as you get into Razor in v5

    http://our.umbraco.org/documentation/v501/Reference/Templating/DynamicModel

    (note that starting with v5, @DynamicModel has been deprecated and is now @CurrentPage ... does the same thing but name makes more sense)

     

    cheers,
    doug. 

Please Sign in or register to post replies

Write your reply to:

Draft