brother
brother

Reputation: 8151

Get mediaId in Umbraco

In Umbraco 7, i am trying to get the Id of a Image on the page. I am using;

@Umbraco.Field("myImage")

But, it does not return the mediaId. Instead it returns;

Umbraco.Web.PublishedCache.XmlPublishedCache.PublishedMediaCache+DictionaryPublishedContent

What am i doing wrong?

Upvotes: 0

Views: 635

Answers (3)

Joel
Joel

Reputation: 21

I have just upgraded to version 7.6.12 and there are some updates with the media picker. To use it as you done you should select the obsolete mediapicker in the backoffice otherwise Model.Content.GetPropertyValue<IPublishedContent>("myImage").Url should work. If you using the UmbracoHelper, you could write UmbracoHelper.AssignedContentItem.GetPropertyValue<IPublishedContent>(propertyName, recursive).Url

https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/

Upvotes: 0

Alex
Alex

Reputation: 2915

There are basically 3 ways:

  • @CurrentPage.myImage
  • @Model.Content.GetPropertyValue<int>("myImage")
  • Using ModelsBuilders you can access it as @Model.myImage

The first method should be avoided because Umbraco will be dropping support for Dynamics (see https://our.umbraco.org/Documentation/Reference/Common-Pitfalls/#dynamics).

ModelsBuilder is my preferred way for accessing published content properties as it provides a strongly typed model based on your Document Type. But it requires some setup. See https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/ for a great intro.

The final option is accessing the properties from @Model.Content (IPublishedContent and unlike the Dyanamics method, will continue to be supported in the next version of Umbraco.

Upvotes: 2

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12505

In Umbraco 7 you can use @CurrentPage. You just need to use:

@CurrentPage.myImage

instead of @Umbraco.Field("myImage") and it will return mediaId

Upvotes: 0

Related Questions