Directory.GetFiles(Server.MapPath()) from dropdown.SelectedValue

615 views Asked by At

I have a photo gallery that with different sets in different folders. Currently i have a different page for each set. What I want to do is use a dropdown to choose which set to display. I'm using

Directory.GetFiles(Server.MapPath("~/path/to/photos")) 

to get all the files from the folder but I can't figure out how to get a variable to work in place of the path. Here is the original code from one of the sets pages

<div class="gallery">
 <div class="row">
            @{foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/photos/halloween"), "*.jpg"))
                { var img = new FileInfo(imgPath);
                    <div class="col-lg-3" style="margin-top:50px">
                        <div id="thumb">

                            <a href="@Href("~/photos/halloween", Path.GetFileName(imgPath))" data-title="Halloween" data-lightbox="Halloween">
                                <img src="@Url.Content(String.Format("~/photos/halloween/{0}", img.Name))" class="img.thumbnail" height="160px" />
                            </a>
                        </div>
                    </div>
                }
            }
        </div>
    </div>
im trying to do something like

foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/photos/" + album.selectedValue + "/"), " *.jpg"))

or

string albumPath = ("~/photos/" + album.selectedValue);
                    foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.jpg"))

I keep getting the error that the variable (within the MapPath) does not exist in the current context. I've tried declaring them in the model and controller. Is there a way to get this working or is there a better way to do this?

Below are the view, controller and model of what I currently trying to get working

View

@model IEnumerable<WebsiteMVC.Models.GalleryModel>
    @{
        ViewBag.Title = "Halloween";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/lightbox.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <head>
        <link href="~/Content/lightbox.css" rel="stylesheet" />
        <style>
            #thumb {
                max-height: 200px;
                max-width: 200px;
            }
        </style>

    </head>

    <div class="container">
        <h2>Halloween 2016</h2>
        <div>
            @Html.DropDownList("album", new List<SelectListItem>
           {
               new SelectListItem { Text ="Halloween", Value="halloween" },
new SelectListItem { Text ="Winter Dance", Value="winterdance" },
new SelectListItem { Text ="Winter Concert", Value="winterconcert" },
new SelectListItem { Text ="Family Work Day", Value="famworkday" },
new SelectListItem { Text ="Valentine's Day", Value="valentinesday" },
new SelectListItem { Text ="Read Across America", Value="readacrossam" },
new SelectListItem { Text ="Family Fitness Night", Value="fitness" },
new SelectListItem { Text ="Aladdin", Value="Aladdin" },
new SelectListItem { Text ="Wizards Game", Value="Wizards" },
new SelectListItem { Text ="Miscellaneous", Value="misc" }
           }, "Select Album", new { htmlAttributes = new { @class = "form-control" }, @onchange = "this.form.submit();", ID = "album" })
        </div>


        <div class="gallery">


            <div class="row">
                
                @{string albumPath = ("~/photos/" + album.selectedValue);
                    foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.jpg"))
                    {
                        var img = new FileInfo(imgPath);


                        <div class="col-lg-3" style="margin-top:50px">
                            <div id="thumb">

                                <a href="@Href("~/photos/halloween", Path.GetFileName(imgPath))" data-title="Halloween" data-lightbox="Halloween">
                                    <img src="@Url.Content(String.Format("~/photos/halloween/{0}", img.Name))" class="img.thumbnail" height="160px" />
                                </a>

                            </div>
                        </div>

                    }
                }


            </div>

        </div>

    </div>

controller

        public ActionResult Gallery(string Album, string albumPath)
        {
            //albumPath = ("~/photos/" + Album);
            return View();



        }

and Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebsiteMVC.Models
{
    public class GalleryModel
    {
        public string Album { get; set; }
        public string albumPath { get; set; }

    }
}

2

There are 2 answers

7
Eric B On

First, you are not returning a model object to the view inside your controller. You need to instantiate your model class, set its properties, and then pass it to the View() method.

public ActionResult Gallery(string Album, string albumPath)
{
    GalleryModel model = new GalleryModel();
    model.albumPath = ("~/photos/" + Album);
    return View(model);
}

Next, you're defining your view's model as an IEnumerable of GalleryModel

@model IEnumerable<WebsiteMVC.Models.GalleryModel>

This makes the view expect a collection of objects. In your case it appears you just want a single gallery displayed in your view, so your @model definition should look like this.

@model WebsiteMVC.Models.GalleryModel

Now you can access properties in your GalleryModel from your view, so you can pass the albumPath from your model into Server.MapPath

foreach (var imgPath in Directory.GetFiles(Server.MapPath(Model.albumPath), "*.jpg"))

Note the usage of Model.albumPath to access the albumPath property on your model.

Finally, you gave these two examples which were not working:

foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/photos/" + album.selectedValue + "/"), " *.jpg"))

string albumPath = ("~/photos/" + album.selectedValue);
foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.jpg"))

In both of these, you're attempting to use the album variable, which has not been defined anywhere. If you meant to use the Album property on your model, you'd need to use Model.Album.

0
Randy On

'HERE IS THE SAMPLE OF DISPLAYING TO GRIDVIEW USING VIRTUAL PATH apply it inside the button or using sub procedures

If Not IsPostBack Then
            Dim filePaths() As String = Directory.GetFiles(Server.MapPath("/UploadedFiles/" + lblfullname.Text))
            Dim files As List(Of ListItem) = New List(Of ListItem)
            For Each filePath As String In filePaths
                files.Add(New ListItem(Path.GetFileName(filePath), filePath))
            Next
            GridView1.DataSource = files
            GridView1.DataBind()
        End If