Copied to clipboard

Flag this post as spam?

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


  • Peter Schermers 112 posts 134 karma points
    Nov 16, 2012 @ 10:42
    Peter Schermers
    0

    Embedded Content doesn't work with .Skip() or .Take()

    Hi,

    I've been trying to get a pagination to work in combination with Embedded Content. It all works fine, except for the part where I'm adding a .Skip and .Take to the foreach loop: it's giving me an error.

    Here's my code:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
        int pageSize; // How many items per page
        int page; // The page we are viewing

        /* Set up parameters */
       
        if (!int.TryParse(Parameter.PageSize, out pageSize))
        {
            pageSize = 3;
        }

        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }

        /* This is your basic query to select the nodes you want */

        var nodes = @Model.producten;
       
        int totalNodes = nodes.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
       
        /* Bounds checking */
       
        if (page > totalPages)
        {
            page = totalPages; 
        }
        else if (page < 1)
        {
            page = 1;
        }
    }

    @foreach (var item in nodes.Skip((page - 1) * pageSize).Take(pageSize))
    {
        <div class="span8">
            <div class="prod_foto">
                <img src="@Library.MediaById(@item.prod_foto.InnerText).Url" />
            </div>
            <div class="prod_tekst">
                <h1>@item.prod_naam.InnerText</h1>
                <p>@item.prod_oms.InnerText</p>
            </div>
        </div>
    }
       
    <div class="pagination pagination-centered">
        <ul>
            @for (int p = 1; p < totalPages + 1; p++)
            {
                string active = (p == page) ? "active" : String.Empty;
                <li class="@active"><a href="?page=@p" title="Go to page @p of results">@p</a></li>
            }
        </ul>
    </div>

    Or am I missing something here?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 16, 2012 @ 11:05
    Jeavon Leopold
    1

    Hi Peter,

    Embedded content stores its content as XML so it is returned as DynamicXml when you use it in a Razor macro, unfortunatly DynamicXml unlike DynamicNode does not currently have the take and skip methods (although it would be great if it did!)

    However you can load your DynamicXml into a List and use the take and skip methods as shown on this post

    Hope that helps

    Jeavon

     

     

     

     

     

  • Peter Schermers 112 posts 134 karma points
    Nov 16, 2012 @ 12:01
    Peter Schermers
    0

    Hi Jeavon,

     

    Thanks for your reply!

    I got it working now with the help of a professional Umbraco developer (and without the .Skip or .Take).

     

    Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft