Copied to clipboard

Flag this post as spam?

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


  • JoskerVemeulen 39 posts 210 karma points
    1 week ago
    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 101 posts 629 karma points
    1 week ago
    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 376 posts 2251 karma points c-trib
    1 week ago
    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 39 posts 210 karma points
    1 week ago
    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