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 11280 posts 23678 karma points MVP 11x 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 473 posts 1183 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 290 posts 500 karma points MVP 8x 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;}

Please Sign in or register to post replies

Write your reply to:

Draft