Copied to clipboard

Flag this post as spam?

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


  • Jules 274 posts 585 karma points
    Dec 31, 2011 @ 03:13
    Jules
    0

    Parameters

    Just started using Razor - I want to pass in a parameter with the name of a property. the property contains a media id.  so I need to get the media item from the media id.

    I have the following but how can I use a parameter in the way described.

    @{
      var media = new umbraco.cms.businesslogic.media.Media(Convert.ToInt32(Model.selectImage));
      string sImgUrl= media.getProperty("umbracoFile").Value.ToString();
      string sImgAlt = media.Text;
      var sImgAttribs = String.Format(" src='{0}' alt='{1}'", sImgUrl, sImgAlt);
      <img@Html.Raw(sImgAttribs) />
     }

    Thanks
    J

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 31, 2011 @ 03:41
    Tom Fulton
    1

    Hi Julian,

    Not tested but I think this might work - change .fieldAlias to the alias of your parameter:

    @{
      var mediaId = Model.GetPropertyValue(@Parameter.fieldAlias)
      var media = new umbraco.cms.businesslogic.media.Media(Convert.ToInt32(mediaId));
      string sImgUrl= media.getProperty("umbracoFile").Value.ToString();
      string sImgAlt = media.Text;
      var sImgAttribs = String.Format(" src='{0}' alt='{1}'", sImgUrl, sImgAlt);
      <[email protected](sImgAttribs) />
     }

    Also I think it has better performance if you use Library.MediaById instead, ie:

    var media = Library.MediaById(mediaId)

    -Tom

  • Jules 274 posts 585 karma points
    Dec 31, 2011 @ 04:02
    Jules
    0

    HI Tom

    works a treat thanks

    J

Please Sign in or register to post replies

Write your reply to:

Draft