<umbraco:Macro runat="server" language="razor"> @using System @using System.Linq @using System.Collections.Generic @using umbraco.MacroEngines @foreach(DynamicXml item in Model.questions.item) { @item
}
</umbraco:Macro>
And here is the error:
Error loading Razor Script Cannot implicitly convert type
'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'.
An explicit conversion exists (are you missing a cast?)
I've even tried doing a "@foreach(System.Collections.IEnumerable item in Model.questions.item)..." and it get the same error. What am I doing wrong?
Model.Questions.Item doesn't work. It says that DynamicXml does not contain a definition for Item. Lowercase works, it's just cast as a DynamicXml class which is not IEnumerable. I see how this syntax would be useful for getting a single node in the xml, but still have no idea how to use it to enumerate across the nodes.
Cannot implicitly convert type 'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Just curious if there is any news on this. @Sebastiaan, your code does not work for me in my project (4.7RC - object does not contain a def for element). I'm realling having a hard time pulling data out of the mnp to validate that the current page exists inside a mnp from another document.
var sectors = Model.Parent.Parent.DescendantsOrSelf("Sector"); foreach (var sector in sectors) { foreach (XElement item in sector.MultiNodePicker.BaseElement.Elements("nodeId")) { <span>@item.Value</span> } }
I've tried that and about every other combination. This is what I get without the MultiNodePicker:
Here is my code:
foreach (XElement item in sector.BaseElement.Elements("nodeId"))
{
@item.Value
}
Here is the error message. All sectors in this case have a multi node picker called projects. All I'm trying to do is determine if the current page is contained inside the other page's mnp. In other cases I need to loop over them and get the nodeid's to display information about that document. Going crazy!
Error loading Razor Script
'umbraco.MacroEngines.DynamicNull' does not contain a definition for 'Elements'
lucuma and now just put your property in between, because now you're doing that on your page element :)
I ended up with the following code for my mutlinodepicker, but yours should work fine too if you put your propertyalias between "sector" and "BaseElement" :)
@{ int i =1; var items = XDocument.Parse( new DynamicNode(Model.Id).AncestorOrSelf("site").GetProperty("serviceNavigation").Value).Descendants().Where(n => n.Name == "nodeId");
int arrLength = items.Count(); }
@foreach (var item in items) { var Node = @Model.NodeById(@item.Value); string css = ""; if (i == arrLength) { css = "last-item"; } <li class="@css"><a href="@Node.Url" title="@Node.Name">@Node.Name</a></li>
I appreciate the help. The failures were happening b/c I needed to check to make sure that MNP had a value before looping over it or you get BaseElement runtime errors. Your code helped in that I am checking sector.GetProperty("projects").Value to make sure it isn't empty before looping. Thanks for your help.
Hi, btw, if you're using uComponents and want to get the collection of nodes picked by a the MultiNodeTreePicker or the XPathCheckBoxList, you can also use:
How to iterate DynamicXml?
Basically, I'm not quite sure how I am supposted to iterate through nodes in the DynamicXml class.
Here is my xml:
<data> <item id="1"> <question propertyid="1">Question 1</question> <answer propertyid="2"><![CDATA[Answer 1]]></answer> </item> <item id="2"> <question propertyid="1">Question 2</question> <answer propertyid="2"><![CDATA[Answer 2]]></answer> </item> <item id="3"> <question propertyid="1">Question 3</question> <answer propertyid="2"><![CDATA[Answer 3]]></answer> </item> </data>
And here is the error:
Error loading Razor Script Cannot implicitly convert type 'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
I've even tried doing a "@foreach(System.Collections.IEnumerable item in Model.questions.item)..." and it get the same error. What am I doing wrong?
Actually, I figured out a way:
Is there a cleaner way to do this, or is this the preferred method?
try Model.Questions.Item i think case is changed event though your doctype alias and property alias is low case.
Regards
Ismail
Model.Questions.Item doesn't work. It says that DynamicXml does not contain a definition for Item. Lowercase works, it's just cast as a DynamicXml class which is not IEnumerable. I see how this syntax would be useful for getting a single node in the xml, but still have no idea how to use it to enumerate across the nodes.
Same here, i need it to loop across the values in a uComponents: Multi-Node Tree Picker which produces the following xml:
My razor file currently looks like this:
Cannot implicitly convert type 'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Gareth is looking into it, for now, this one works for me:
Just curious if there is any news on this. @Sebastiaan, your code does not work for me in my project (4.7RC - object does not contain a def for element). I'm realling having a hard time pulling data out of the mnp to validate that the current page exists inside a mnp from another document.
Lucuma, try leaving out "MultiNodePicker", should work :)
I've tried that and about every other combination. This is what I get without the MultiNodePicker:
Here is my code:
foreach (XElement item in sector.BaseElement.Elements("nodeId")) { @item.Value }
Here is the error message. All sectors in this case have a multi node picker called projects. All I'm trying to do is determine if the current page is contained inside the other page's mnp. In other cases I need to loop over them and get the nodeid's to display information about that document. Going crazy!
lucuma and now just put your property in between, because now you're doing that on your page element :)
I ended up with the following code for my mutlinodepicker, but yours should work fine too if you put your propertyalias between "sector" and "BaseElement" :)
@Gerty -
I appreciate the help. The failures were happening b/c I needed to check to make sure that MNP had a value before looping over it or you get BaseElement runtime errors. Your code helped in that I am checking sector.GetProperty("projects").Value to make sure it isn't empty before looping. Thanks for your help.
Hi, btw, if you're using uComponents and want to get the collection of nodes picked by a the MultiNodeTreePicker or the XPathCheckBoxList, you can also use:
HTH,
Hendy
Do you want it simple?
Got this from Jedi-Master Dirk De Grave
@x.InnerText
our.umbraco.org/..../21753-@foreach-value-of-Multiple-Textstring-uComponents-
/Johan
is working on a reply...