Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben Schlaepfer 74 posts 101 karma points
    Aug 26, 2013 @ 11:13
    Ben Schlaepfer
    0

    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.

    <a target="_blank" href="@Model.Media("dL2dataSheets","umbracoFile")">@Model.dL2dataSheets</a>

     

    I want the link text to display the name and tried a few stabs in the dark:

    @Model.Media("dL2dataSheets","Name")
    @Model.Media("dL2dataSheets","umbracoName")

     

    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

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 26, 2013 @ 12:10
    Jeavon Leopold
    0

    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

  • Ben Schlaepfer 74 posts 101 karma points
    Aug 26, 2013 @ 13:34
    Ben Schlaepfer
    0

    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!).

     @if (Model.HasValue("dL5performanceData"))
              {   
                <tr>
                  <td width="20%">Performance Data</td>
                  <td width="80%"><a target="_blank" href="@Model.Media("dL5performanceData","umbracoFile")">@Model.Media("dL5performanceData","Name")</a></td>
                </tr>   
            } 

     

    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?

       <tr>
                  <td width="20%">Performance Data</td>
                  <td width="80%"><a target="_blank" href="/media/18951/10-005-A3-AH PD.pdf"></a></td>
                </tr> 
    
            

     

    Thanks again for your help...

    Ben 

     

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 26, 2013 @ 13:50
    Jeavon Leopold
    0

    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

    var selectedMedia2 = Model.Media("dL5performanceData");
      <img src="@selectedMedia2.umbracoFile" width="@selectedMedia2.umbracoWidth" height="@selectedMedia2.umbracoHeight" alt="@selectedMedia2.Name"/>
    

    Jeavon

  • Ben Schlaepfer 74 posts 101 karma points
    Aug 26, 2013 @ 14:13
    Ben Schlaepfer
    1

    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:

    @if (Model.HasValue("dL5performanceData"))
    {
    var dl5 = Model.Media("dL5performanceData");

    <tr>
    <td width="20%">Performance Data</td>
    <td width="80%"><a href="@dl5.umbracoFile" target="_blank">@dl5.Name</a></td>
    </tr>
    }

     

    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

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Aug 26, 2013 @ 16:25
    Jeavon Leopold
    0

    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

    @{      
        if (CurrentPage.HasValue("dL5performanceData")){                                         
            var dynamicMediaItem = Umbraco.Media(CurrentPage.dL5performanceData);
            <a href="@dynamicMediaItem.umbracoFile" target="_blank>@dynamicMediaItem.Name</a>
        }
    }
    

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft