Copied to clipboard

Flag this post as spam?

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


  • ianhoughton 281 posts 605 karma points c-trib
    Mar 03, 2013 @ 23:40
    ianhoughton
    0

    Razor groupby or take ?

    I have the following code and am trying to get the nodes grouped into 2's. I'm not sure how to change startNode.Children to achieve it. I've tried take(2) but this obviously stops after 2 !! So the outer foreach needs to group in 2's with those being displayed in the inner UL.

    if (startNode.Children.Where("Visible").Any())
        {
            <div id="cycleslider" class="cycleslider">
                @foreach (var page in startNode.Children.Where("Visible"))
                {
                    <div>
                        <ul>
                            <li class="product">
                                <a href="@page.Url"><img src="/css/images/productImage.png" alt="" /></a>
                                <span>Price</span>
                                <span class="discount">£199</span>
                                <a href="@page.Url" class="button blue">Deal<br/>£159</a>
                                <a href="@page.Url" class="button green">Buy Now</a>
                            </li>
                        </ul>
                    </div>
                }
            </div>
        }

    This is the outcome I really want:

    if (startNode.Children.Where("Visible").Any())
        {
            <div id="cycleslider" class="cycleslider">
                @foreach (var page in startNode.Children.Where("Visible"))
                {
                    <div>
                        <ul>
                            <li class="product">
                                <a href="@page.Url"><img src="/css/images/productImage.png" alt="" /></a>
                                <span>Price</span>
                                <span class="discount">£199</span>
                                <a href="@page.Url" class="button blue">Deal<br/>£159</a>
                                <a href="@page.Url" class="button green">Buy Now</a>
                            </li>
                            <li class="product">
                                <a href="@page.Url"><img src="/css/images/productImage.png" alt="" /></a>
                                <span>Price</span>
                                <span class="discount">£199</span>
                                <a href="@page.Url" class="button blue">Deal<br/>£159</a>
                                <a href="@page.Url" class="button green">Buy Now</a>
                            </li>
                        </ul>
                    </div>
                }
            </div>
        }
  • Carsten Fallesen 35 posts 154 karma points
    Mar 04, 2013 @ 06:28
    Carsten Fallesen
    0

    Hi,

    I think you are looking for the InGroupsOf functionality. The use of it is described on this documentation page.  I use that to do something similar to what you are trying to achieve.

    /Carsten

Please Sign in or register to post replies

Write your reply to:

Draft