NoseBagUK
NoseBagUK

Reputation: 375

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

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

Upvotes: 1

Views: 3379

Answers (4)

atazmin
atazmin

Reputation: 5707

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



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

Upvotes: 0

prjseal
prjseal

Reputation: 246

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;

Upvotes: 3

NoseBagUK
NoseBagUK

Reputation: 375

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;
}

Upvotes: 2

Harsheet
Harsheet

Reputation: 628

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

Upvotes: 3

Related Questions