Copied to clipboard

Flag this post as spam?

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

  • dominik 711 posts 733 karma points
    Jul 17, 2012 @ 13:52

    Razor script example

    Is there any razor script example (cshtml) how i can loop through the items? 


  • Tim 56 posts 78 karma points
    Mar 25, 2013 @ 02:02

    I'm sure I'm not the only one who needed this. Ok so I've spent a considerable amount of time trying to work this out, and here it is

    Example XML of my repeatable custom content



    <data alias="bannerImage">1053</data>

    <data alias="bannerTitle">My Title 1</data>



    <data alias="bannerImage">1054</data>

    <data alias="bannerTitle">My Title 2</data>



    Razor script to see my repeatable custom content called homepageBanner

    var xml = @Library.ToDynamicXml(@Model.GetPropertyValue("homepageBanner"));

     Razor scripting to output bannerTitle

    @using System
    @using System.Xml.Linq;
    @using System.Linq
    @using System.Collections.Generic
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
           var xml = Library.ToDynamicXml(@Model.GetPropertyValue("homepageBanner"));
        foreach(XElement item in xml.BaseElement.Elements("item")) {    
            foreach(XElement data in item.Elements("data"))
                 if (data.Attribute("alias").Value == "bannerTitle")
    } }


    My Title 1
    My Title 2

    It's not very elegant but it works :) To be honest it's far easier to do it xslt but I wanted to have everything in Razor.

  • Andy 10 posts 31 karma points
    May 09, 2013 @ 22:50

    @Tim: worked great for me! Thanks!

  • Marc-Anthony Taylor 55 posts 68 karma points
    Jul 11, 2013 @ 09:35
    Marc-Anthony Taylor

    Hi! Anyone have an idea how to do this in Umbraco v6 in an MVC project?

  • Marc-Anthony Taylor 55 posts 68 karma points
    Jul 12, 2013 @ 12:23
    Marc-Anthony Taylor

    Ok, I worked out a solution that works with v6.

        XmlDocument xmlDoc = new XmlDocument();
        XmlNodeList nodes = xmlDoc.GetElementsByTagName("item");

    and then:

    foreach (XmlNode xNode in (from XmlNode dataNode in nodes select dataNode.SelectNodes("data") into n where n != null from XmlNode a in n select xNode).Where(inText => !String.IsNullOrEmpty(inText.InnerText)))
    { // code here }
  • Yadwinder singh 43 posts 112 karma points
    Jul 18, 2013 @ 08:23
    Yadwinder singh

    Hi! Anyone have an idea how to do this in umbaco v6 using Razor view?


    <html><div class"row"> <div class="span4 carshow"></div>
    <div class="span4 carshow"></div>
    <div class="span4 carshow"></div></div><div class="row"> <div class="span4 carshow"></div>
    <div class="span4 carshow"></div>
    <div class="span4 carshow"></div>
  • Lars-Erik Aabech 349 posts 1100 karma points MVP 6x c-trib
    Jul 18, 2013 @ 09:17
    Lars-Erik Aabech

    @Tims example should work with only small changes in both 4.0 and 6.0 MVC views.

    Just add @using System.Linq.Xml to the top of the view.

    Instead of

    var xml =Library.ToDynamicXml(@Model.GetPropertyValue("homepageBanner"));

    you can do

    var doc = XDocument.Parse(Model.Content.GetPropertyValue("homepageBanner"));
    // or
    var doc = XDocument.Parse(CurrentPage.homepageBanner.ToString());

    foreach(var item in doc.Elements("item") { ... }
Please Sign in or register to post replies

Write your reply to: