dVery confusing on how Umbraco did this, and I'm looking for some sort of explanation if possible. I'm using Umbraco 7. I have a custom property
on a doc type
that is a True/False
(basically a check box).
I'm receiving an error when attempting to parse the GetPropertyValue
, object not reference to an object
, which is the True/False
checkbox.
I do NOT get the error when parsing the 1
INTO an object.
Code:
var topics = topicDocType.Select(x => new ForumModel
{
Topic = x.Name,
TopicId = x.Id,
/*no error*/ IsClosedForQuestions = x.GetPropertyValue("closedQuestions") == (object)1 ? true : false,
Questions = x.Descendants().Where(y => y.DocumentTypeAlias.Equals("Question")).Select(y => new Question
{
QuestionName = y.GetPropertyValue("question").ToString(),
QuestionId = y.Id,
QuestionDateTime = y.CreateDate,
AskedBy = y.GetPropertyValue("askedBy").ToString(),
/*no error*/ IsClosedForPosts = y.GetPropertyValue("closedPosts") == (object)1 ? true : false,
Posts = y.Descendants().Where(z => z.DocumentTypeAlias.Equals("Post")).Select(z => new Post
.... more code here
This doesn't work, and I do not get why:
var topics = topicDocType.Select(x => new ForumModel
{
Topic = x.Name,
TopicId = x.Id,
/*doesn't work*/ IsClosedForQuestions = int.Parse(x.GetPropertyValue("closedQuestions").ToString()) == 1 ? true : false,
.... more code here
This doesn't work either:
var topics = topicDocType.Select(x => new ForumModel
{
Topic = x.Name,
TopicId = x.Id,
/*doesn't work*/ IsClosedForQuestions = x.GetPropertyValue("closedQuestions").ToString() == "1" ? true : false,
.... more code here
If I create a test variable
var test = new ContentService().GetById(2269).GetValue("closedQuestions");
The value of test
is 0
, b/c the check box isn't checked. If I check it, run it again, the value is 1
.
This could just be a C# question that perhaps I knew nothing about, and nothing really to do w/ Umbraco, but I know if I .ToString()
an object and attempt to check if the value is equal to another string or not, it works.
Appreciate it.
EDIT:
After reading what Umbraco saves true/false as an int. True/False built-in-prop
True/False is a simple checkbox which saves either 0 or 1, depending on the checkbox being checked or not.
I also looked up how to parse correctly from an object(int)
Better way to cast object int
However, this is still not working
IsClosedForQuestions = (int)x.GetPropertyValue("closedQuestions") == 1 ? true : false,
EDIT 2:
This DOES work, with retrieving the correct value. Any answers as to why?
IsClosedForQuestions = x.GetPropertyValue<int>("closedQuestions") == 1 ? true : false,
To show you that it's not null:
After change to int.Parse(....).ToString()) == 1
It seems that x.GetPropertyValue("closedQuestions")
is returning null
in some cases (or maybe an int?
with a null value), which is why you're getting that error when trying to call ToString()
on it. If x.GetPropertyValue<int>("closedQuestions")
works for you then that's great, but I wanted to point out one thing:
You should not compare value-types (like int
) by casting them to object and using ==
. Casting to object will box the value type and use referential equality which will fail:
object o1 = 1;
object o2 = 1;
Console.WriteLine(o1 == o2); // will print `false`
You could instead use object.Equals()
:
IsClosedForQuestions = object.Equals(x.GetPropertyValue("closedQuestions"),1)