Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Is there any razor script example (cshtml) how i can loop through the items?
Thanks
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
<items>
<item>
<data alias="bannerImage">1053</data>
<data alias="bannerTitle">My Title 1</data>
</item>
<data alias="bannerImage">1054</data>
<data alias="bannerTitle">My Title 2</data>
</items>
Razor script to see my repeatable custom content called homepageBanner
var xml = @Library.ToDynamicXml(@Model.GetPropertyValue("homepageBanner")); @xml
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") { @data.Value } } } }
Output:
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.
@Tim: worked great for me! Thanks!
Hi! Anyone have an idea how to do this in Umbraco v6 in an MVC project?
Ok, I worked out a solution that works with v6.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(milestoneIds.Value.ToString());
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 }
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></div></html>
@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"));// orvar doc = XDocument.Parse(CurrentPage.homepageBanner.ToString());foreach(var item in doc.Elements("item") { ... }
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Razor script example
Is there any razor script example (cshtml) how i can loop through the items?
Thanks
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
<items>
<item>
<data alias="bannerImage">1053</data>
<data alias="bannerTitle">My Title 1</data>
</item>
<item>
<data alias="bannerImage">1054</data>
<data alias="bannerTitle">My Title 2</data>
</item>
</items>
Razor script to see my repeatable custom content called homepageBanner
Razor scripting to output bannerTitle
Output:
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.
@Tim: worked great for me! Thanks!
Hi! Anyone have an idea how to do this in Umbraco v6 in an MVC project?
Ok, I worked out a solution that works with v6.
and then:
Hi! Anyone have an idea how to do this in umbaco v6 using Razor view?
@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
you can do
is working on a reply...