Copied to clipboard

Flag this post as spam?

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


  • greengiant83 88 posts 109 karma points
    Jun 15, 2011 @ 04:40
    greengiant83
    0

    Accessing nested properties with razor

    I have an image property on my document that ends up getting saved like this in the xml

     

    <mainImage>
              <DAMP fullMedia="">
                <mediaItem>
                  <Image id="1095" version="9a2cb21b-a142-4053-a15a-3e7afc7bfb70" parentID="-1" level="1" writerID="0" nodeType="1032" template="0" sortOrder="54" createDate="2011-06-14T21:10:02" updateDate="2011-06-14T21:10:02" nodeName="MountainstopExp002" urlName="mountainstopexp002" writerName="admin" nodeTypeAlias="Image" path="-1,1095">
                    <umbracoFile>/media/406/mountainstopexp002.jpg</umbracoFile>
                    <umbracoWidth>2304</umbracoWidth>
                    <umbracoHeight>3072</umbracoHeight>
                    <umbracoBytes>3576676</umbracoBytes>
                    <umbracoExtension>jpg</umbracoExtension>
                  </Image>
                </mediaItem>
              </DAMP>
            </mainImage>

    Using razor, how can I access mainImage/DAMP/mediaItem/Image/umbracoFile?

     

  • Toni Becker 146 posts 425 karma points
    Jun 15, 2011 @ 12:03
    Toni Becker
    0

    inside your template or i prefer to use a macro .cshtml write the following:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @foreach (dynamic c in Model.mediaItem)
    {
             @: <img src="@c.umbracoFile" alt="" />
    }

    The @: says that this is Markup code, so don't be irritated by that.

    Thats all.

    For more details read inside this post http://our.umbraco.org/forum/developers/razor/18993-DAMP-concerned-retrieving-Crop-url-@-Sebastiaan

    Greetings





  • greengiant83 88 posts 109 karma points
    Jun 15, 2011 @ 14:47
    greengiant83
    0

    Toni,

    Thanks for the reply. Your code appears to want to loop over all mediaItem's in the current document. Unfortunately, this isnt an option here. I have other properties on the document that are also images. Also I do know that there is only one image here. Is there a way to access as a singular item

  • Toni Becker 146 posts 425 karma points
    Jun 16, 2011 @ 14:01
    Toni Becker
    0

    Okay that's easy. Insert the following code into your template:

    <umbrac:Macro language="cshtml" runat="server">
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @:<img src="@Model.mainImage.mediaItem.Image.umbracoFile" alt="" />
    </umbraco:Macro>

    Have fun with that piece of code :)

Please Sign in or register to post replies

Write your reply to:

Draft