Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 karma points
    Nov 13, 2017 @ 10:29
    Dmitriy
    0

    How to get content by index in Enumerable?

    I'm trying to get content by index from set, but it does't compile.

    Example:

    var set = Model.MyContent; // it is a NestedContent!
    for (int i = 0; i < set.Count(); i++)
     {
         <p>@set[i].MyProperty</p> // Error! 
     }
    

    Why?! Enumerable

    Does better way exists? All I want - render my content in rows like that :

    Row 1
    - item 1
    - item 2
    - item 3
    Row 2
    - item 4
    - item 5
    - item 6
    

    I tryed to use .Next() method, but it does,t works with NestedContent for some reason.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 13, 2017 @ 10:52
    Alex Skrypnyk
    0

    Hi Dmitriy

    What version of Umbraco are you using?

    "NestedContent" is it "IEnumerable

    Thanks,

    Alex

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 13, 2017 @ 10:53
    Alex Skrypnyk
    1

    Try this code:

    var set = Model.MyContent.ToList();
    
  • Dmitriy 168 posts 588 karma points
    Nov 14, 2017 @ 08:15
    Dmitriy
    0

    Thanks, Alex. It works!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 13, 2017 @ 11:23
    Dan Diplo
    101

    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>
    }
    
  • Dmitriy 168 posts 588 karma points
    Nov 14, 2017 @ 08:15
    Dmitriy
    0

    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:

    <section>
        @foreach (var row in Model.MyContent.InGroupsOf(3))
        {
            <div class="row">
                @foreach (MyModel item in row)
                {
                    <div class="col-xs-12 col-md-4 col-sm-4">
                        <p>@item.MyProperty</p>
                    </div>
                }
            </div>
        }
    </section
    
Please Sign in or register to post replies

Write your reply to:

Draft