5

I'm new to Umbraco version 5 and Razor, but I'm trying to get the path for a media file stored as a property of the current page so that I can render it as an image.

Searching Google and Stack Overflow have got me this far:

@{
    var mediaId = DynamicModel.Animation;
    var media = (TypedEntity)Umbraco.GetEntityById(mediaId);
}      

where 'Animation' is the name of the media property in my page.

How can I get to the image path of my media item?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Bird
  • 3,764
  • 2
  • 28
  • 38
  • 1
    UPDATE: For anyone starting out with Umbraco v5 - be aware that v5 has been discontinued (http://umbraco.com/follow-us/blog-archive/2012/6/13/v5-rip.aspx) – Rob Bird Jun 20 '12 at 21:54

2 Answers2

6

You need to use the @Umbraco.GetMediaUrl helper method. In my case:

<img src="@Umbraco.GetMediaUrl(DynamicModel.myImageProperty)" />

where myImageProperty is the name of the property in my page.

I hope this helps someone.

(OK, I found the answer to my own question, seems I was too lazy to study the sample book store site in detail which explains why there wasn't more information on the web.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Bird
  • 3,764
  • 2
  • 28
  • 38
  • You, sir, are a genius. I've been battling this for the last two hours. –  Mar 25 '12 at 10:28
  • Anyone know how to do this for a media picker in version 7? @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "umbLayout.cshtml"; } @Umbraco.RenderMacro("InsertWidgit", new {mediaFile=@CurrentPage.mediaFile}) – tree Aug 01 '14 at 02:28
1

DynamicModel is deprecated and will be removed in Umbraco 5.3, instead of it you should use CurrentPage:

<img src="@Umbraco.GetMediaUrl(CurrentPage.imageProperty)" />

jasin_89
  • 1,993
  • 7
  • 30
  • 41