Nested content is of type IEnumerable<IPublishedContent>. The IEnumerable interface doesn't support accessing items by index. In order to access by index you need to convert to either an Array or a List that does support addressing by index ie.
var items = Model.MyContent.ToList();
or
var items = Model.MyContent.ToArray();
However, it sounds like you want to break your content into equally sized groups? In that case, use the InGroupsOf() extension method. Something like:
var groups = Model.MyContent.InGroupsOf(3);
foreach (var group in groups)
{
<h4>Group</h4>
<ul>
@foreach (var item in group)
{
<li>@item.Name</li>
}
</ul>
}
How to get content by index in Enumerable?
I'm trying to get content by index from set, but it does't compile.
Example:
Why?! Enumerable
Does better way exists? All I want - render my content in rows like that :
I tryed to use .Next() method, but it does,t works with NestedContent for some reason.
Hi Dmitriy
What version of Umbraco are you using?
"NestedContent" is it "IEnumerable
Thanks,
Alex
Try this code:
Thanks, Alex. It works!
Nested content is of type
IEnumerable<IPublishedContent>
. TheIEnumerable
interface doesn't support accessing items by index. In order to access by index you need to convert to either an Array or a List that does support addressing by index ie.or
However, it sounds like you want to break your content into equally sized groups? In that case, use the
InGroupsOf
() extension method. Something like:Dan, Thanks a lot! That is exactly what I wanted! Yes, I realy wanted to group my content in equel groups and then render it!
I'm using Bootstrap 3 without flexboxes, so it hard to render grid when you have different height of content.
For other with same problem, I just leave solution here:
is working on a reply...