Copied to clipboard

Flag this post as spam?

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


  • JoskerVemeulen 68 posts 262 karma points
    May 14, 2019 @ 14:33
    JoskerVemeulen
    0

    limited list - see all link

    Hi,

    I need to show subitems of a node. A simple foreach would do the trick. But i need a limited amount. Max 5 items. If there are more the 5 items it should show a "see all" link.(parent node). If only 3 subitems, the "see all" node should not bu shown ofcourse. Working in v8

    Any help appreciated!

  • Rhys Hamilton 140 posts 942 karma points
    May 14, 2019 @ 14:45
    Rhys Hamilton
    0

    You could use a simple counter variable, like so - and then apply whatever logic you need based on this. For example, if the counter is 5, then show "see more".

    @{
        int counter = 1;
    
        foreach (var i in Model.Children)
        {
            // Apply your own logic based on the counter
            // e.g - if counter == 5, show see all
    
            counter++;
        }
    }
    
  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 15, 2019 @ 07:56
    Søren Kottal
    100

    Hi Josker

    How about this?

    @foreach (var child in Model.Children.Take(5))
    {
      // show the node
    }
    @if (Model.Children.Count() > 5)
    {
      <a href="/show-all-link">Show all</a>
    }
    
  • JoskerVemeulen 68 posts 262 karma points
    May 15, 2019 @ 08:05
    JoskerVemeulen
    0

    Hi Søren,

    Exactly how I did it! I was under the impression that the Take() only works if there are more items. But I was wrong. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft