@Umbraco.TypedMedia(MyProperty) is null in Umbraco 7.6.1

こ雲淡風輕ζ 提交于 2019-12-11 06:25:02

问题


I'm using Umbraco 7.6.1 and I'm trying to get the url for a media image using the code below in razor.

@{
   var bannerMediaItem = Umbraco.TypedMedia(Model.Content.Image); 
   var bannerUrl = bannerMediaItem.Url;
}

But bannerMediaItem is always null even though I know it exists. If I do this..

@Model.Content.Image

It returns the id of the image e.g. 1086


回答1:


In Umbraco v7.6 and above, there is now a new thing called UDI. Find out more about UDI here. https://our.umbraco.org/documentation/reference/querying/Udi

So instead of your media item having a numeric id, by default it will have an id like this: umb://media/39d3ac707d634953ab52642d5037855c

Here is how you get the media url when the id is one of the new type:

string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("headerImage").Url;



回答2:


try this

var propertyValue = content.GetPropertyValue<int>(propertyName); // this will give you the id of the media item
var media = helper.TypedMedia(propertyValue);

var url = media == null ? string.Empty : media.Url;

propertyName (string value) is the name of the property you have for your banner.

content is the IPublishedContent

helper is the UmbracoHelper that you will get from Umbraco.Web

Thanks




回答3:


Ok so I was nearly right and with help from @Harsheet this worked...

@{
   var media = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("image"));
   var thumbUrl = media.Url;
}



回答4:


var pageTitleBackground = (Model.Content.GetPropertyValue<string>("pageTitleBackground")) !=null ? Umbraco.TypedMedia((Model.Content.GetPropertyValue<int>("pageTitleBackground"))).Url : String.Empty;



style="background-image: url('@pageTitleBackground');


来源:https://stackoverflow.com/questions/44958831/umbraco-typedmediamyproperty-is-null-in-umbraco-7-6-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!