获取Child Item的图像ID

问题描述 投票:0回答:1

我正在尝试获取子项的图像属性的ID,因此我可以使用Umbraco.Media()来显示图像。

我正在尝试使用强类型模型来使用最佳实践,我想我已经在这里正确地调用了强类型模型。

为什么以下代码在'var imageId = location.LocationImage.Id;'处返回'对象引用未设置为对象的实例'错误?线?

谢谢。

@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"/>
}

有关信息,我启用了AppData ModelsMode并重建了模型。我的Location.generated.cs中的实现是:

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

umbraco
1个回答
0
投票

这是在Umbraco论坛上解决的,所以我会在这里发布答案。

使用Umbraco强类型模型,您可以将值内联传递给html标记,例如:

<img src="@location.LocationImage.Url"/>

而不是必须将其传递给服务。

© www.soinside.com 2019 - 2024. All rights reserved.