Copied to clipboard

Flag this post as spam?

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


  • Travis 7 posts 58 karma points
    May 05, 2013 @ 21:06
    Travis
    0

    GetPropertyValue for True False

    Hi

    I created a new razor partial view in Umbraco 6. I am trying to compare the value of a True/False field but get an error.

    If I write the True/False value to screen I get the word True. But if I try to compare on that word it fails.

    The code is:
    @if (item.GetPropertyValue("myTrueFalseField") == "True")
    {
    Do something
    }

    Nothing happens, if I try this I get a script error:
    @if (item.GetPropertyValue("myTrueFalseField").ToString() == "True")
    {
    Do something
    }

    I think I am missing something.

    Thanks

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 05, 2013 @ 21:22
    Dennis Aaen
    0

    Hi Travis,

    Maybe I ask silly, but have you tried this variant:


    @if (item.GetPropertyValue("myTrueFalseField") = 1)
    {
    Do something
    }

    /Dennis

  • Travis 7 posts 58 karma points
    May 05, 2013 @ 21:29
    Travis
    0

    Hi Dennis

    No that doesn't work. It complains that comparing object with int is not valid.

    That does raise the point though that GetPropertyValue returns "object" ... not "string". I am not sure how it is possible to write this object to the screen, but it is. It comes out as "True".

    Thanks

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 05, 2013 @ 21:41
    Dennis Aaen
    0

    Hi Travis,

    Have you tried to see if you can use the HasValue method. I must mention that I am pretty new to razor too.

    @if(Model.HasValue("myTrueFalseField")=1)
    {
    Do something
    }

    /Dennis

  • Travis 7 posts 58 karma points
    May 05, 2013 @ 21:53
    Travis
    101

    Hi Dennis

    Yes I solved it thanks!!!! You were almost right and got me on the right track.

    My code was in a loop and ended up like this:

    @foreach (var item in myHomeNode.Children())
    {
    if (item.HasValue("myTrueFalseField"))
    {
    Do something
    }

    Thanks

  • Charles Afford 1163 posts 1709 karma points
    May 05, 2013 @ 22:06
    Charles Afford
    0

    Travis this looks unsafe for what your are trying to do?

    What i would expect to see is something like:

    //out parameter

    int myTrueFalseFieldValue;

     

    @foreach (var item in myHomeNode.Children())

    //check if property exsists and parsing the bool property

    if(item.HasProperty("myTrueFalseField") && Int32.TryParse(item.GetProperty("myTrueFalseField").Value.ToString(), out myTrueFalseFieldValue)

    {

    myTrueFalseFieldValue = 0 // this is false

    myTrueFalseFieldValue = 1 // this is true.

    }

    That is alot safer :).  Hope this helps.  Charlie

  • Arlan 58 posts 184 karma points
    May 12, 2015 @ 21:25
    Arlan
    0
    @if(item.GetPropertyValue("myTrueFalseField")==true)
    {
    Do something
    }

    this worked for me
  • Anja Beisel 8 posts 28 karma points
    Jun 14, 2015 @ 23:31
    Anja Beisel
    0

    In a partial view macro the following test for the value of a property worked for me:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage   
    @{
        string nodeid = Model.MacroParameters["nodeid"].ToString();
        var page = Umbraco.TypedContent(nodeid);
    }
    if(page.GetPropertyValue("code") != null) 
    {
        <div class="sinfoCode">
                 @Html.Raw(page.GetPropertyValue("code"))
        </div>
    }
    
  • Leonardo Moura 21 posts 90 karma points
    Jan 17, 2018 @ 20:59
    Leonardo Moura
    0

    Dears,

    There is a simple way to do it:

    if(item.GetPropertyValue<bool>("yourTrueFalseField")){
         //if it is true do something here
    } else {
         //else it is false do something here
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft