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>
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; }
}
}
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.
Next, you're defining your view's model as an
IEnumerableofGalleryModelThis 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.
Now you can access properties in your
GalleryModelfrom your view, so you can pass thealbumPathfrom your model intoServer.MapPathNote the usage of
Model.albumPathto access thealbumPathproperty on your model.Finally, you gave these two examples which were not working:
In both of these, you're attempting to use the
albumvariable, which has not been defined anywhere. If you meant to use theAlbumproperty on your model, you'd need to useModel.Album.