Razor - @Model. to display media item NAME as anchor link text
Hi guys,
Umbraco v6.0.6 (Assembly version: 1.0.4898.16909)
New to Umbraco with Razor, but very keen to learn.
I have a load of PDF files in the media library that I want to show for any given node that references them from a media picker field (alias dL2dataSheets)
I am able to retrieve the values of the URL and the media item ID as follows.
Thanks for that. I am working within the page template with inline macros at the moment while I test things out: it's just a bit quicker for me and matches more with my front-end leaning and experience :-) I will look at MVC templates though, but slightly wary of the learning curve at this point in time... (no MVC knowledge).
I started with the page you have pointed me to and did try those examples but did not achieve what I was hoping to, my first port of call was as follows:
(This seems to follow the first option on the second method .... an option that has no ALT tag for me to reference name!).
I will look into the MVC views now for my templates, as I understand I will not need to wrap code in <umbraco:Macro runat="server" language="cshtml"> tags.
Yes and no, you can use @ directly in the template as they are cshtml files, however some of the methods have changed, so for instance to do what you have just done you would do something like
@{
if (CurrentPage.HasValue("dL5performanceData")){
var dynamicMediaItem = Umbraco.Media(CurrentPage.dL5performanceData);
<a href="@dynamicMediaItem.umbracoFile" target="_blank>@dynamicMediaItem.Name</a>
}
}
Razor - @Model. to display media item NAME as anchor link text
Hi guys,
Umbraco v6.0.6 (Assembly version: 1.0.4898.16909)
New to Umbraco with Razor, but very keen to learn.
I have a load of PDF files in the media library that I want to show for any given node that references them from a media picker field (alias dL2dataSheets)
I am able to retrieve the values of the URL and the media item ID as follows.
I want the link text to display the name and tried a few stabs in the dark:
But these returned nothing, I have had a dig about looking at cheat sheets and other posts but drawn a blank.
Any help or guidance would be gratefully received!
Cheers
Ben
Hi Ben,
Take a look at the Razor Macros examples here The second method gets the name as the alt text.
As you have just started out, I'm wondering is there a reason you are using Razor macro rather than Mvc templates?
Thanks,
Jeavon
Hi Jeavon,
Thanks for that. I am working within the page template with inline macros at the moment while I test things out: it's just a bit quicker for me and matches more with my front-end leaning and experience :-) I will look at MVC templates though, but slightly wary of the learning curve at this point in time... (no MVC knowledge).
I started with the page you have pointed me to and did try those examples but did not achieve what I was hoping to, my first port of call was as follows:
(This seems to follow the first option on the second method .... an option that has no ALT tag for me to reference name!).
The results contain an empty anchor tag. There appears to be a name in the media library (see below) or is the property"Name" incorrect, I wonder?
Thanks again for your help...
Ben
Hi Ben,
You definitely shouldn't fear Mvc, for what you are doing it is very similar, probably easier as the templates are already cshtml.
Anyway, you will need to use option2 for the Razor macro, e.g
Jeavon
Hi Jeavon,
Your advice has sorted out the problem I was having. Thank you!
To use the name of the Media Item in an inline Razor macro to create a link to a PDF file I used your snippet of code and amended it to:
I will look into the MVC views now for my templates, as I understand I will not need to wrap code in <umbraco:Macro runat="server" language="cshtml"> tags.
I can just whack in my @... notation?
Thanks again for your superfast help.
Ben
Perfect!
Yes and no, you can use @ directly in the template as they are cshtml files, however some of the methods have changed, so for instance to do what you have just done you would do something like
Jeavon
is working on a reply...