Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Aug 18, 2014 @ 17:14
    Amir Khan
    0

    Razor IsOdd for row building logic skipping last node if odd number

    Hi!

    I have this logic to build some rows / columns for a split column layout. It seems that the logic skips the last one if there is an odd number in the total collection. Is there an easy way to check if the total number is odd and change the logic?

    Thanks,
    Amir

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    var selection= CurrentPage.Awards.Where("Visible");
    var totalChildren = @selection.Count();

    int rows = totalChildren / 2;

    }

    @if(selection.Any()){

    <div class="col two">
    @foreach(var page in selection.Take(@rows)){
    <section class="award">
    <h1>project title</h1>
    </section>

    }
    </div>
    <div class="col two last">
    @foreach(var page in selection.Skip(@rows).Take(@rows)){
    <h1>project title</h1>
    }
    </div>
    }

     

  • Amir Khan 1287 posts 2744 karma points
    Aug 18, 2014 @ 17:44
    Amir Khan
    0

    Here we go! int rows = totalChildren / 2 + Math.Sign(totalChildren % 2);

    From Rodion's answer here: http://our.umbraco.org/forum/developers/razor/29168-Rounding-up-a-variable

     

    -Amir

Please Sign in or register to post replies

Write your reply to:

Draft