Copied to clipboard

Flag this post as spam?

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


  • René Pjengaard 114 posts 675 karma points c-trib
    May 23, 2011 @ 22:28
    René Pjengaard
    0

    Razor and embedded content

    Hi there,

    i have a problem i can´t seem to get around. I use Razor and embedded content. When i wan´t my embedded content for the current node, i use .XPath("//billeder/data/item") on @Model. But my problem is, that i get all the embedded content nodes, and not only the content from the current node. Why is that, and is there a way to do this?

    Kind regards
    René

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Consolas; color: #b3251e}

  • Sebastiaan Janssen 4893 posts 14611 karma points MVP admin hq
    May 24, 2011 @ 07:13
    Sebastiaan Janssen
    1

    In XPath, when you use "//" it means look for items from the root, instead of in the current path. However, the "root" of @Model should be the current page and not the whole cache.. Weird, I should test this. For now, you can just use "/billeder/data/item", right? Or @Model.billeder.data.item (I don't know what the embedded content xml output looks like though).

  • René Pjengaard 114 posts 675 karma points c-trib
    May 24, 2011 @ 07:18
    René Pjengaard
    0

    Thought the same as you Sebastian. But apparently thats not the case. So .XPath("billeder/data/item") works for me now. Thanks for the effort.

    /René

  • Jeroen Breuer 4861 posts 12134 karma points MVP 3x admin c-trib
    May 24, 2011 @ 11:54
    Jeroen Breuer
    1

    Why would you want to use an XPath for this? In Razor you can work much easier with Embedded Content. Here is an example:

    Here is the Embedded Content xml:

    <projectUrls>
      <data>
        <item id="1">
          <url propertyid="1">http://www.project2.nl</url>;
          <title propertyid="2">Project 2</title>
        </item>
        <item id="2">
          <url propertyid="1">http://project3.nl</url>;
          <title propertyid="2">Project 3</title>
        </item>
        <item id="3">
          <url propertyid="1">http://project4.nl</url>;
          <title propertyid="2">Project 4</title>
        </item>
      </data>
    </projectUrls>

    Here is my Razor code:

    <ul>
        @*Show the url's that are selected with the embedded content package.*@
        @foreach (dynamic item in Model.projectUrls)
        {
            <li>
                <a href="@item.url" target="_blank">@item.title</a>
            </li>
        }
    </ul>

    Jeroen

  • René Pjengaard 114 posts 675 karma points c-trib
    May 24, 2011 @ 21:50
    René Pjengaard
    0

    Wow thanks Jeroen, that was just what i was looking for. I´m quite new to Razor, so thanks alot for the example. Sadly enough, i have allready voted for MVP :-)

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Jun 08, 2011 @ 10:51
    Rasmus Berntsen
    0

    Perfect, just what I was looking for... :)

  • Tony Kiernan 278 posts 341 karma points
    Sep 29, 2011 @ 15:30
    Tony Kiernan
    0

    OK, I followed Joreon's post and had this working. I've updated the site from 4.7.0 to 4.7.1 and it's stopped working

    My code:

     var embedded = Model.Embedded;
    foreach (dynamic item in embedded)
    {
    <p>
    @item.Selected.ToString()
    </p>
    }

    My XML

    <Embedded>
    <data>
    <item id="1">
    <Selected propertyid="2">1195</Selected>
    </item>
    <item id="2">
    <Selected propertyid="2">1177</Selected>
    </item>
    </data>
    </Embedded>

    Am I missing something?

  • Jeroen Breuer 4861 posts 12134 karma points MVP 3x admin c-trib
    Sep 29, 2011 @ 15:34
    Jeroen Breuer
    0

    The dynamic xml has been changed (improved) in 4.7.1. You can read all about it in this blogpost: http://umbraco.com/follow-us/blog-archive/2011/9/15/umbraco-razor-feature-walkthrough%E2%80%93part-6.aspx.

    Jeroen

  • Tony Kiernan 278 posts 341 karma points
    Sep 29, 2011 @ 15:38
    Tony Kiernan
    0

    OK, this works

    @item.InnerText.ToString()

    But, surely if I had more than one attribute in there it wouldn't?

  • Tony Kiernan 278 posts 341 karma points
    Sep 29, 2011 @ 16:30
    Tony Kiernan
    0

    Will give that a read

  • Funka! 398 posts 660 karma points
    Oct 13, 2011 @ 05:25
    Funka!
    0

    @Tony Kiernan Thank you for this hint. So we now need to deal with the @item in your example (and our actual case!) as a DynamicXml now which is being treated and working differently.  The InnerText property seems OK if we wanted to deal with a giant flattened string, but we don't, so now the challenge is to find out how to work around this and drill into our tree. (Unforunately, the "Razor Feature Walkthough" has no suitable examples or API published for us to reference, and we are left guessing as to how to query our EmbeddedContent data nodes in a convenient manner.) For example, there is an InnerText, as you showed, but no such InnerXml nor dynamic properties to use instead??

    What we are really missing at this point is a "here is how to convert stuff (Razor over EmbeddedContent) that used to work in 4.7.0 to work now in 4.7.1". I've tried probably dozens of different combinations and guessing now at property/method names and now I guess next step is to download Umbraco source and find out what DynamicXml's API is...?

    Thank you for your help!

  • Funka! 398 posts 660 karma points
    Oct 13, 2011 @ 20:11
    Funka!
    0

    I perhaps should start a new topic for this, since I didn't realize at first this is marked as "solved" already per a five-month old original message...

  • gilad 185 posts 425 karma points
    Dec 08, 2011 @ 13:26
    gilad
    0

    this is work for me :

    @item.propertyAlias.InnerText.ToString()

  • Marc Burton 5 posts 28 karma points
    Jan 10, 2012 @ 13:00
    Marc Burton
    0

    The XPath method in razor will give you access to the whole cache, so querying from the root (ie //somenode) will query from the root of your site not the current node as you'd expect in XSLT. Removing those preceding slashes fixes this

  • bob baty-barr 1180 posts 1294 karma points MVP
    Aug 15, 2012 @ 15:42
    bob baty-barr
    0

    how would i go about getting a SPECIFIC node in the embedded content? i am new to razor and still don't quite get it all.

    thanks,

Please Sign in or register to post replies

Write your reply to:

Draft