Copied to clipboard

Flag this post as spam?

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


  • James 251 posts 1169 karma points
    Dec 19, 2014 @ 12:13
    James
    0

    True/False Umbraco 6

    Hello everyone,

     

    I am having an issue with a piece of code.

    @if(Model.HasValue("myTrueFalseCheckBox")

    {

    <img src="url here" alt="alt">

    }

    This code displays an image whether the box is checked or not. If i check the box. The image is still displayed. If the box is unchecked, the image is displayed.

    Am i doing something wrong here?

    Its an umbraco six build that I am working on. Cheers.

     

    James.

     

     

     

     

     

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Dec 19, 2014 @ 12:20
    Jan Skovgaard
    101

    Hi James

    I think you should try writing your if statement like this

    @if(!String.IsNullOrEmpty(Model.myTrueFalseCheckBox)){ //image code }

    See the example here http://odetocode.com/blogs/scott/archive/2011/06/28/a-better-razor-isnullorempty-statement.aspx

    Hope this helps.

    /Jan

  • James 251 posts 1169 karma points
    Dec 19, 2014 @ 12:20
    James
    0

    I appear to have fixed it :

     

    @if(Model.myTrueFalseCheckBox)

    {

    Do Something

    }

     

    Thanks anyway.

     

    Cheers

     

    James.

  • John C Scott 457 posts 1157 karma points
    Dec 19, 2014 @ 12:21
    John C Scott
    1

    HasValue will be true if the property is contained in the data. Checking HasValue first will stop a null error if the property does not exist in the node you are looking at.

    Something like: 

    @if(Model.HasValue("myTrueFalseCheckBox") && Model.myTrueFalseCheckBox == 1)   // i'm not actually sure if it is equal to 1 when true

    might be what you're looking for. Not actually given you the answer but hopefully explained why it is always showing the picture.

  • Ravi Motha 289 posts 499 karma points MVP 4x c-trib
    Dec 19, 2014 @ 12:24
    Ravi Motha
    0

    because the property is a true or false.. the property will always have a value.. you need to test on what the actual value is

    if (Model.GetProperty("myTrueFalseCheckBox").Value ){ true}else {false;}

  • 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