Within views, I'm retrieving content using the below method. However, if the selected content becomes unpublished, I'm receiving the yellow screen of death with the exception "Object reference not set to an instance of an object.". Is there a better way to retrieve content from a model to avoid this from happening?
I tried using if (Home.HasProperty("b2c")) {}, but the issue is that this check returns true because it does have that property assigned with a value, the content value is just unpublished.
From my experiences .HasValue() dosen't really work on IPublishedContent. You would have to create a variable and do a null check.
Maybe something like this would work then?
var B2C =Home.GetProperty("b2c");
if(B2C != null)
{
Umbraco.Content(B2C.Value);
}
Since I don't know that much about what you are trying to achieve. I am just guessing you are tying to get the value from a content picker? Couldn't you do something like this then?
var B2C = Home.Value<IPublishedContent>("b2c");
if(B2C != null)
{
B2C.Name
}
View YSOD
Within views, I'm retrieving content using the below method. However, if the selected content becomes unpublished, I'm receiving the yellow screen of death with the exception "Object reference not set to an instance of an object.". Is there a better way to retrieve content from a model to avoid this from happening?
IPublishedContent B2C = Umbraco.Content(Home.GetProperty("b2c").Value
you could check if it has a value before trying to use it.
I tried using if (Home.HasProperty("b2c")) {}, but the issue is that this check returns true because it does have that property assigned with a value, the content value is just unpublished.
Try checking the IPublishedContent.IsDraft property as in
if (!item.IsDraft) { }
Hi there,
From my experiences
.HasValue()
dosen't really work onIPublishedContent
. You would have to create a variable and do a null check.Maybe something like this would work then?
Since I don't know that much about what you are trying to achieve. I am just guessing you are tying to get the value from a content picker? Couldn't you do something like this then?
Hope this helps.
//Johannes
is working on a reply...