Copied to clipboard

Flag this post as spam?

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


  • David 16 posts 127 karma points
    Dec 19, 2018 @ 23:47
    David
    0

    Display div using an If statement, when a checkbox is 'checked'.

    I am trying to show a div if a checkbox is checked. Before, ? 0 : 1 worked for me when I made a lambda expression. On this occasion, however, on an if statement, the div I wish to 'show/hide' based on the checkbox still shows, even when it is not checked.

    @if (Model.Content.Name == "Biography" || 
     Model.Content.Name == "biography" || 
     Model.Content.Name == "About" || 
     Model.Content.Name == "about" && 
     Model.Content.GetPropertyValue<bool>("ShowSignUpForm"))
    {
      <div class="c-article__region">
         Test
      </div>
    }
    

    I have also tried another code line I saw on Our:

    @if(Model.Content.GetPropertyValue("ShowSignUpForm")==true)
    {
       <div>Test</div>
    }
    

    On the above, I get the exception

    Cannot apply operator "" to operands of type 'object' and 'bool'.

    But with no luck. Any idea what I am doing wrong?

    Thanks very much

    -David

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 20, 2018 @ 05:52
    Jan Skovgaard
    0

    Hi David

    If you only render the @Model.Content.GetPropertyValue("ShowSignUpForm) what does it then output? Does it output "True" or 0?

    If it does output "True" then please try writing your second if statement like

    @if(Model.Content.GetPropertyValue("ShowSignUpForm).ToString() == "True"){
    <div>test</div>
    }
    

    I hope this helps!

    /Jan

  • louisjrdev 107 posts 344 karma points c-trib
    Dec 20, 2018 @ 07:36
    louisjrdev
    1

    Hi David,

    Alternatively you can try:

    @if(Model.Content.GetPropertyValue<bool>("ShowSignUpForm"))
    {
       <div>Test</div>
    }
    

    This should now work as you are making the returned result strongly typed as a boolean, meaning you don't even need an == true comparison.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 20, 2018 @ 08:54
    Jan Skovgaard
    0

    Do'h! I should have suggested that initially really - I must not have been fully awake earlier ;-)

    /Jan

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Dec 20, 2018 @ 10:29
    Nik
    0

    Hi David,

    I think the issue is more to do with the operators and their behaviour. Generally if you are using Or's and And's you need to wrap the groupings in parenthesis. If it was me, I'd restructure the if to be something more like this:

    @if ((Model.Content.Name.Equals("biography", StringComparison.InvariantCultureIgnoreCase) || 
         Model.Content.Name.Equals("about", StringComparison.InvariantCultureIgnoreCase))  && 
         Model.Content.GetPropertyValue<bool>("ShowSignUpForm"))
    {
         <div class="c-article__region">
             Test
         </div>
    }
    

    What this is then doing is:

    1) If the Name is equal to Biography or About, irrelevant of case

    AND

    2) If ShowSignUpForm is true

    THEN

    Display Div with Test text.

    Does that work for you?

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft