Why is there no simple answer to this!?! - Razor, Parameter, Media
Hello,
I have been looking through the all the information I can find about how to do this, it really should not take 2 hours of research to get this to work, very frustrating.
Simple need.
I have a macro, macro has a parameter for a mediaCurrent item. I want to put a link to the media in my html via Razor.
I have tried the Walkthroughs, I have tried searching the forum, I have tried searching google. Doesn't seem it should be so hard to find answer.
Perhaps walkthroughs should include reading media from parameter and actually getting its url, I did not see this. And by this I mean a clear example of how to get the value out of the parameter and load the media with it, instead of several pieces of sample code not directly correlated to each other.
Code I am trying to get to work:
@using umbraco.MacroEngines @{ var description = Parameter.description; var marketingContentID = Parameter.marketingContent; var thumbnailImageID = Parameter.thumbnailImage; dynamic marketingContent = Model.Media(marketingContentID); dynamic thumbnailImage = Model.Media(thumbnailImageID); }
Sorry I can't give a full answer right now, but it you convert your thumbnailImage value to an int, you can use library.GetMedia, with an additional boolean parameter 'deep'.
Thsi will give you an XPathNodeIterator object, if you select the first tem and then the Value property, you will have the path to the image. I'll post an example code t lunchtime today.
n.b. you use Model.Media("propertyName") when you have a property on your current model containing an ID which should be loaded as a media item. So let's say you have catImage on your document type, you'd want to go Model.Media("catImage").Url to get the URL for the cat.
You use Library.MediaById (which is also aliased as Model.MediaById but that's deprecated now) when you have an ID number (or that ID comes from querystring) and you want to get the media item.
In short, Funka! is right but I thought I'd supply a bit more detail on why.
Why is there no simple answer to this!?! - Razor, Parameter, Media
Hello,
I have been looking through the all the information I can find about how to do this, it really should not take 2 hours of research to get this to work, very frustrating.
Simple need.
I have a macro, macro has a parameter for a mediaCurrent item. I want to put a link to the media in my html via Razor.
I have tried the Walkthroughs, I have tried searching the forum, I have tried searching google. Doesn't seem it should be so hard to find answer.
Perhaps walkthroughs should include reading media from parameter and actually getting its url, I did not see this. And by this I mean a clear example of how to get the value out of the parameter and load the media with it, instead of several pieces of sample code not directly correlated to each other.
Code I am trying to get to work:
@using umbraco.MacroEngines
@{
var description = Parameter.description;
var marketingContentID = Parameter.marketingContent;
var thumbnailImageID = Parameter.thumbnailImage;
dynamic marketingContent = Model.Media(marketingContentID);
dynamic thumbnailImage = Model.Media(thumbnailImageID);
}
<div class="box"><a href="@marketingContent.umbracoFile"><img src="@thumbnailImage.umbracoFile" alt="" /></a><br />
<h2>Description</h2>
@description
</div>
Hi radmanmm,
Maybe try this?
@{
...
var marketingContent = Library.MediaById(marketingContentID);
var thumbnailImage = Library.MediaById(thumbnailImageID);
}
Hopefully this helps. The "Library" is an intrinsic helper that I have been using in my own hacked-together solutions. Best of luck!
P.S., there is a lovely cheat-sheet (just found it on my desk to reference here for you) which is where I first learnt of this, that you can get at http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet ...
Again, best of luck!
Hi radmanmm,
Sorry I can't give a full answer right now, but it you convert your thumbnailImage value to an int, you can use library.GetMedia, with an additional boolean parameter 'deep'.
Thsi will give you an XPathNodeIterator object, if you select the first tem and then the Value property, you will have the path to the image. I'll post an example code t lunchtime today.
HTH
Tom
Funka! - YOU ROCK!!! thank you so much, this was killing me!
n.b. you use Model.Media("propertyName") when you have a property on your current model containing an ID which should be loaded as a media item.
So let's say you have catImage on your document type, you'd want to go Model.Media("catImage").Url to get the URL for the cat.
You use Library.MediaById (which is also aliased as Model.MediaById but that's deprecated now) when you have an ID number (or that ID comes from querystring) and you want to get the media item.
In short, Funka! is right but I thought I'd supply a bit more detail on why.
is working on a reply...