Copied to clipboard

Flag this post as spam?

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


  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 11:12
    Rayyan
    0

    Path.GetFileName(item) - Control the number of items

    Hi,

    Is there a way to control the number of the items that the code get from the folder?

    Something like this;

     <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item) [6 Items Max] " />
    

    Many thanks

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 20, 2017 @ 11:15
    Alex Skrypnyk
    1

    Hi Rayyan

    This code, that you provided, doesn't return files from the folder.

    This is what you need:

    string[] files = Directory.GetFiles(folderPath).Take(6).ToArray();
    

    Thanks,

    Alex

  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 11:28
    Rayyan
    0

    Thank you Alex,

    I just realise that I didn't ask the right question, what i'm looking for is to limit the number of the thumbnails only and keep the foreach as it is.

    Is that possible?

     <img src="/media/@Model.Content.GetPropertyValue("placeID")/@Path.GetFileName(item) **[take(6)]** " />
    

    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/" + Model.Content.GetPropertyValue("placeName"));
    }
    @foreach (string item in files){
    
    <li id="firstImage" class="col-xs-6 col-sm-4 col-md-3" data-src="/media/@Model.Content.GetPropertyValue("placeName")/@Path.GetFileName(item)" data-sub-html="">
                        <a href="">
                            <img src="/media/@Model.Content.GetPropertyValue("placeName")/@Path.GetFileName(item)"/>
                        </a>
                    </li>
    } 
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 20, 2017 @ 11:55
    Alex Skrypnyk
    0

    Rayyan, use this code:

    @{
        string folderPath = Server.MapPath("/media");
        string[] files = Directory.GetFiles(folderPath + "/" + Model.Content.GetPropertyValue("placeName")).Take(6).ToArray();
    }
    @foreach (string item in files){
    
        <li id="firstImage" class="col-xs-6 col-sm-4 col-md-3" data-src="/media/@Model.Content.GetPropertyValue("placeName")/@Path.GetFileName(item)" data-sub-html="">
            <a href="">
                <img src="/media/@Model.Content.GetPropertyValue("placeName")/@Path.GetFileName(item)"/>
            </a>
        </li>
    } 
    
  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 12:05
    Rayyan
    0

    This code limits the foreach to display 6 items, and what I want is to keep the foreach as it is and limiting the img tag only.

    Thanks

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 20, 2017 @ 12:06
    Alex Skrypnyk
    100

    How many img tags do you have now?

  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 12:09
    Rayyan
    0

    6 img tags

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 20, 2017 @ 12:53
    Alex Skrypnyk
    0

    You will have the same number of img tags on your page as images.

  • Rayyan 46 posts 238 karma points
    Jul 20, 2017 @ 13:01
    Rayyan
    0

    You;re right, but i want to let the foreach displays all the

  • and limit the img tag. And the source should looks like this;

    <li data-src="/media/806.png" data-sub-html="">
                            <a href="">
                                <img class="img-responsive" src="/media/806.png"/>
                            </a>
                        </li>
        <li data-src="/media/853.png" data-sub-html="">
                            <a href="">
                                <img class="img-responsive" src="/media/853.png"/>
                            </a>
                        </li>
        <li data-src="/media/089.png" data-sub-html="">
                            <a href="">
                            </a>
                        </li>
        <li data-src="/media/931.png" data-sub-html="">
                            <a href="">
                            </a>
                        </li>
        <li data-src="/media/061.png" data-sub-html="">
                            <a href="">
                            </a>
                        </li>
        <li data-src="/media/735.png" data-sub-html="">
                            <a href="">
                            </a>
                        </li>
        <li data-src="/media/637.png" data-sub-html="">
                            <a href="">
                            </a>
                        </li>
    

    As you can see, only the first 2 img tag (for example) are displayed.

Copy Link
Please Sign in or register to post replies

Write your reply to:

Draft