Copied to clipboard

Flag this post as spam?

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


  • Chris 3 posts 106 karma points
    Apr 17, 2019 @ 10:07
    Chris
    2

    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.

    @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:

    /// Location Image [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.0.4")] [ImplementPropertyType("locationImage")]
    public Image LocationImage => this.Value("locationImage");
    

    Arrrgh! :)

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Apr 17, 2019 @ 12:10
    Matt Barlow | jacker.io
    100

    With strongly typed, you don't need to pass the id into a service.

    Should just be:

    if (location.LocationImage != null)
    {
        <img src="@location.LocationImage.Url" alt="" />
    }
    
  • Dave de Moel 122 posts 574 karma points c-trib
    Apr 17, 2019 @ 13:00
    Dave de Moel
    1

    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").

  • Chris 3 posts 106 karma points
    Apr 17, 2019 @ 13:18
    Chris
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft