Copied to clipboard

Flag this post as spam?

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


  • Joshua McBride 5 posts 85 karma points
    Jun 09, 2018 @ 17:06
    Joshua McBride
    0

    Checkbox True False If Statement

    I am new to working with Umbraco.

    I have a checkbox inside a document type.

    The field is called Hero Image Filter and it's intended purpose will allow the user to add a preset filter to the hero image, or remove it.

    The checkbox is selected, and returns a value of True when the following command is placed inside the template

    @Umbraco.Field("homeheroImageFilter")
    

    When I create a variable as follows, it still returns the correct value

    @{var filter = Umbraco.Field("homeheroImageFilter");}
    
    @filter
    

    Now I want to use the variable to create an if statement, where if the condition is true it shows a

    tag and content.

    This returns a compilation error

    @{var filter = Umbraco.Field("homeheroImageFilter");}
    
    @filter
    
    @if(filter == "True"){<p>Test</p>}
    

    This also returns a compilation error

    @{var filter = Umbraco.Field("homeheroImageFilter");}
    
    @filter
    
    @if(@filter == "True"){<p>Test</p>}
    

    But when I change the if condition as follows, it works as expected..

    @if("True" == "True"){<p>Test</p>}
    

    So, what am I doing wrong? All help would be appreciated!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 09, 2018 @ 17:23
    Dennis Aaen
    0

    Hi Joshua, and welcome to Umbraco :)

    Have a look here how to work with the true / false

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/True-False

    Hope this helps,

    /Dennis

  • Joshua McBride 5 posts 85 karma points
    Jun 09, 2018 @ 17:30
    Joshua McBride
    0

    Hi Dennis,

    Thanks for the link, but can you help me modify this code to display the value I need returning?

    Thanks

    J

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 09, 2018 @ 18:02
    Jan Skovgaard
    0

    Hi Joshua and also welcome to the Our forum :)

    You need to change this

    @{var filter = Umbraco.Field("homeheroImageFilter");}
        @if(filter == "True")
        {
              <p>Test</p>
        }
    

    Into this and then it should work for you

    @{var filter = Umbraco.Field("homeheroImageFilter");}
        @if(filter.ToString() == "True"){
            <p>Test</p>
        }
    

    Here you make sture to convert whatever the type of value you get back in your variable to a string and therefore you are now comparing two strings and won't see the yellow screen of death.

    I hope this makes sense?

    /Jan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 09, 2018 @ 18:10
    Dennis Aaen
    100

    Hi Joshua,

    Here is a piece of code that you can use. So if the checkbox is checked then you will get the value printed out.

    @if (Model.Content.GetPropertyValue<bool>("homeheroImageFilter")){
            {
                <p>@Model.Content.Name</p>
            }
        }   
    

    A little explanation. Model.Content is refer to the page that you are viewing in the browser. Its the same when you are using the

    @Umbraco.Field("homeheroImageFilter")
    

    So make sure that the page that you are viewing in the browser has the property on the page. Else you will need to do some traversing.

    Hope this helps,

    /Dennis

  • Joshua McBride 5 posts 85 karma points
    Jun 10, 2018 @ 09:33
    Joshua McBride
    0

    Thanks guys!

Please Sign in or register to post replies

Write your reply to:

Draft