Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Razor
@{
    ViewBag.Title = "GalerijaIndex";
    Layout = "~/Views/Shared/_GalerijaLayout.cshtml";
}

<div id="mycarousel" class="carousel slide" data-ride="carousel" style="margin-top: -15px">

    <ol class="carousel-indicators">
        <li data-target="#mycarousel" data-slide-to="0" class="active"></li>
        <li data-target="#mycarousel" data-slide-to="1"></li>
        <li data-target="#mycarousel" data-slide-to="2"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <img src="~/Content/First/Music1.jpg" />
        </div>
        @{
            string source = "~/Content/Images/";
            string[] fileEntries = Directory.GetFiles(source);
            foreach (var i in fileEntries)
            {
                <div class="item">
                    <img src="~/Content/Images/@{Path.GetFileName(i)}" />
                </div>
             }
        }
             }


    <a class="left carousel-control" href="#mycarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Prethodna</span>
    </a>

    <a class="right carousel-control" href="#mycarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Slijedeća</span>
    </a>
</div>


The goal is to populate slider dynamically with all images in "~Content/Images/" folder, so that it can display them. All the logic is here on View, yet I can't figure out why I'm getting 500 error.

What I have tried:

I've tried what I known, but I have no idea what is wrong. Everything else works fine. I have tried pass value to it via ViewBag from controller, same thing;
Posted
Updated 4-Nov-16 4:40am
v4
Comments
Richard Deeming 4-Nov-16 10:03am    
Neither do we, because you haven't told us what the problem is.

Click "Improve question" and update your question with a proper description of the problem. Include the full details of any errors.
1suli0 4-Nov-16 10:41am    
I'm sorry sir, won't happen again.

1 solution

You still haven't told us what the error is, but at a guess, it probably comes from these two lines:
Quote:
string source = "~/Content/Images/";
string[] fileEntries = Directory.GetFiles(source);

The Directory.GetFiles method expects the physical path to the folder. You are passing in an app-relative virtual path. Since the GetFiles method knows nothing about ASP.NET applications, you will most likely get a DirectoryNotFoundException.

To fix that, you need to map the virtual path to a physical path:
C#
string source = "~/Content/Images/";
string physicalPath = System.Web.Hosting.HostingEnvironment.MapPath(source);
string[] fileEntries = Directory.GetFiles(physicalPath);

NB: This sort of code really belongs in the controller, not the view.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900