I'm trying to get the ID of a child item's image property so I can use Umbraco.Media() to display the image.
I have two doctypes Locations (parent) and Location (child).
I'm using v8 and trying to use best practice by using strongly-typed models. I think I've called the strongly-typed models correctly here.
Why is the following code returning the 'Object reference not set to an instance of an object' error at the 'var imageId = location.LocationImage.Id;' line?
What's wrong with my syntax? (for info, Umbraco.TypedMedia is now deprecated and my intellisense doesn't provide GetPropertyValue).
Thank you.
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ContentModels.Locations>
@using ContentModels = Umbraco.Web.PublishedModels;
@{
Layout = "master.cshtml";
var locations = Model.Children<Location>();
}
@foreach (var location in locations)
{
<p>@location.LocationName</p> //this returns a string as expected
var imageId = location.LocationImage.Id; //this throws the error
var image = Umbraco.Media(imageId);
<img src="@image"/>
}
Location.LocationImage isn't null...
if (location.LocationImage != null)
{
<p>@location.LocationImage</p>
}
...returns the string 'Umbraco.Web.PublishedModels.Image'.
I've enabled AppData ModelsMode and rebuilt the models: For info, the implementation in my Location.generated.cs is:
Do all your locations have an image and are they all published? The error you get suggests that there is at least one Location that does not have an image, thus it will throw the error when trying to access the ID (basically you are doing "null.Id").
Get Image ID of Child Item (strongly typed)
Hi.
I'm trying to get the ID of a child item's image property so I can use Umbraco.Media() to display the image.
I have two doctypes Locations (parent) and Location (child).
I'm using v8 and trying to use best practice by using strongly-typed models. I think I've called the strongly-typed models correctly here.
Why is the following code returning the 'Object reference not set to an instance of an object' error at the 'var imageId = location.LocationImage.Id;' line?
What's wrong with my syntax? (for info, Umbraco.TypedMedia is now deprecated and my intellisense doesn't provide GetPropertyValue).
Thank you.
Location.LocationImage isn't null...
...returns the string 'Umbraco.Web.PublishedModels.Image'.
I've enabled AppData ModelsMode and rebuilt the models: For info, the implementation in my Location.generated.cs is:
Arrrgh! :)
With strongly typed, you don't need to pass the id into a service.
Should just be:
Do all your locations have an image and are they all published? The error you get suggests that there is at least one Location that does not have an image, thus it will throw the error when trying to access the ID (basically you are doing "null.Id").
Thank you Dave de Moel, you make a good point and that could have been a cause here.
However, Matt Barlow's response was the correct one in this case. Calling the value directly worked.
Though I'm a bit surprised it did. I was unable to assign the value, so didn't expect it to work inline.
This works:
<img src="@location.LocationImage.Url"/>
Whilst this throws an error:
var imageId = location.LocationImage.Url;
How odd! What's going on here then? If anyone is able to comment, please feel free.
Anyway, thank you for your responses. I've marked Matt's answer as the solution here.
is working on a reply...