Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 04, 2011 @ 16:10
    Jeroen Breuer
    0

    Razor variable property

    Hello,

    I've got a razor script that I want to make variable. Here is my script:

    <img src="@DAMP_Helper.GetImageCropperUrl(Model.infoBox1Image.mediaItem, "container")" width="268" height="151" border="0" alt="@Model.infoBox1Image.mediaItem.ImageHomepageContainer.alt" class="styled" />

    I want the infoBox1Image to be variable. For example it could also be Model.infoBox2Image.mediaItem. Is this possible? Can I make a variable (which is filled with an @parameter) and do Model.variable.mediaItem? 

    Jeroen

  • Alex 78 posts 136 karma points
    May 04, 2011 @ 16:14
    Alex
    0

    You could try to us getProperty. This is untested.

    Model.getProperty(@Parameter.PropName).mediaItem
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 04, 2011 @ 16:24
    Jeroen Breuer
    0

    Model.getProperty doesn't seem to work. If getProperty could work somehow would .mediaItem afterwards still work or do I need to cast the value to something dynamic again?

    Jeroen

  • Alex 78 posts 136 karma points
    May 04, 2011 @ 16:27
    Alex
    0

    Sorry, it should be capital G. Model.GetProperty()

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 04, 2011 @ 16:32
    Jeroen Breuer
    0

    Thanks. It works, but I can't use .mediaItem afterwards. If I try this:

    Model.GetProperty("infoBox1Image").mediaItem

    I get the following exception:

     

    'Model.GetProperty("infoBox1Image").mediaItem' threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' dynamic {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}

    Guess I need to convert it back to something again.

     

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 04, 2011 @ 17:49
    Jeroen Breuer
    1

    I got it working with the following code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    @using umbraco;
    @using System.Linq;
    @using System.Xml.Linq;
    @using DigibizAdvancedMediaPicker;

    @{
    <img src="@DAMP_Helper.GetImageCropperUrl(DynamicXml(Model.GetProperty(@Parameter.PropName)).mediaItem, "container")" width="268" height="151" border="0" alt="@Model.infoBox1Image.mediaItem.ImageHomepageContainer.alt" class="styled" />
    }

    @functions
    {
    dynamic DynamicXml(umbraco.NodeFactory.Property xml)
    {
    return new umbraco.MacroEngines.DynamicXml(XElement.Parse(xml.Value));
    }
    }

    I add a DynamicXml function because I'm going to use it a lot more. It will be moved to another file, but is here for the example :).

    Jeroen

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 04, 2012 @ 16:35
    Sebastiaan Janssen
    0

    Refactored (for 4.7.1), this would look like:

    @using DigibizAdvancedMediaPicker
    <img src="@DAMP_Helper.GetImageCropperUrl(Library.ToDynamicXml(Model.GetProperty(Parameter.PropName)).mediaItem, "container")" width="268" height="151" border="0" alt="@Model.infoBox1Image.mediaItem.ImageHomepageContainer.alt" class="styled" />
    
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 26, 2012 @ 16:35
    Jeroen Breuer
    0

    Just tested Sebastiaan his example, but it should be Model.GetPropertyValue(Parameter.PropName) instead.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft