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".
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
Hi Travis,
Maybe I ask silly, but have you tried this variant:
/Dennis
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
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.
/Dennis
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
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
this worked for me
In a partial view macro the following test for the value of a property worked for me:
Dears,
There is a simple way to do it:
is working on a reply...