0

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; }

    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eric F
  • 11
  • 1

2 Answers2

1

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.

Eric B
  • 691
  • 5
  • 11
  • Thanks for the help so far, I made the changes and it is storing the proper path into "Model.albumPath" when I select from the dropdown list. Now I just cant get it to load the images onto the page. I put a breakpoints on the "foreach" line and the "var img =..." but it never breaks at "var img =...". It's not going into the loop. – Eric F Nov 16 '17 at 21:17
  • @EricF I notice in the second parameter of `Directory.GetFiles` you're passing `" *.jpg"` (note the space before the *). I suspect you want to just pass `"*.jpg"` to get all jpg files in that directory. I'll update my answer to reflect this. – Eric B Nov 17 '17 at 15:46
  • good catch. It is going into the loop now but I an getting the error "...' is a physical path, but a virtual path was expected." so "albumPath" is getting the correct virtual path but "ImgPath" is getting the physical path of the image. – Eric F Nov 17 '17 at 17:45
  • The paths returned by `Directory.GetFiles` will be physical paths, not virtual paths, so there is no need to call `Server.MapPath` on the `imgPath` value. Can you share the specific line of code that is throwing this error? – Eric B Nov 20 '17 at 16:08
  • I get the error on 'var img = new FileInfo(imgPath);' what I don't understand is that I'm using the same code currently with the exception that I have a static location i.e. '~/photos/halloween' instead of trying to use the variable 'albumPath' and its working. – Eric F Nov 21 '17 at 17:51
  • Have you tried using the debugger to step through the code and see what value the `albumPath` variable has at the time the error occurs? If it works for a hard-coded location, I would suspect the variable does not have the correct value. – Eric B Nov 21 '17 at 21:35
  • in the `foreach (var imgPath.....` line `Model.albumPath` is `~/photos/` which is correct but when it gets to `var img = new FileInfo(imgPath);` i get the error. in both the variable version and the hard coded version at `var img = new FileInfo(imgPath);` `imgPath` is a physical path not virtual but it only gives the error in the variable version. – Eric F Nov 27 '17 at 16:22
1

'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
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
Randy
  • 11
  • 1