Reputation: 57159
I manage to get my media URL using the following syntax:
Umbraco.Media(CurrentPage.MyImage).Url
But, when I try the same using the GetProperty
syntax (strangely, I used getProperty
for a while from examples online, but that syntax always returned some empty dynamic object), which works fine for other properties, I get all kinds of casting errors:
// results in:
// System.InvalidOperationException: The value of parameter 'id' must
// be either a string or an integer
Umbraco.Media(CurrentPage.GetProperty("myImage")).Url
// or
Umbraco.Media(CurrentPage.GetProperty("_myImage")).Url
And using some form of Umbraco.TypedContent<Media>
led to only more NRE and casting errors. I ultimately tried the following, but that returned an empty list always:
Umbraco.Media("myImage","umbracoFile")
I don't mind using the dynamic syntax, or any other syntax for that matter, but I am surprised that GetProperty
apparently returns something different than the dynamic syntax.
Ultimately, all I wanted is to use the _propName
syntax (underscore-prefixed name) to get the property of this, or any parent page, whichever is set. Short of querying the AncestorOrSelf
list of pages, is there some way to get this to work using "simple" syntax?
Quick update
If I do @CurrentPage.GetProperty("_defaultBackgroundImage")
I see the media id in the HTML, but when I try to feed it to Umbraco.Media
I get InvalidOperationException
the value of parameter 'id' must be either a string or an integer. Casting to int
give invalid cast and using ToString
returns the type name. There must be some way to get back the same integer value I see as when I output it using Razor @
-syntax, right?
Upvotes: 3
Views: 3623
Reputation: 6050
From the comments above:
What you are looking for is getting the property value recursively.
Since you are using umbraco 7
instead of accessing CurrentPage
that is of type dynamic
I would suggest you use strongly typed Model.Content
property which is of type IPublishedContent
.
Than intellisense will tell you all the methods available, one of which is:
Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("myImage", recursive: true, ifCannotConvert: 0))
and it will work.
The same goes for Model.Content.GetProperty
method.
Upvotes: 3