【发布时间】:2017-11-03 18:39:50
【问题描述】:
我有一个照片库,在不同的文件夹中有不同的集合。目前,我为每组设置了不同的页面。我想要做的是使用下拉菜单来选择要显示的集合。我正在使用
Directory.GetFiles(Server.MapPath("~/path/to/photos"))
从文件夹中获取所有文件,但我不知道如何让变量代替路径工作。这是其中一组页面的原始代码
<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"))
或
string albumPath = ("~/photos/" + album.selectedValue);
foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.jpg"))
我不断收到该变量(在 MapPath 中)在当前上下文中不存在的错误。我试过在模型和控制器中声明它们。有没有办法让它工作或有更好的方法来做到这一点?
下面是我目前尝试工作的视图、控制器和模型
查看
@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>
控制器
public ActionResult Gallery(string Album, string albumPath)
{
//albumPath = ("~/photos/" + Album);
return View();
}
和模型
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; }
}
}
【问题讨论】:
-
我添加了完整的代码。是的专辑是下拉菜单
-
告诉我们当前上下文中不存在什么变量。告诉我们变量的名称。告诉我们在哪里。
-
在这种情况下......“名称'专辑'在当前上下文中不存在”
标签: c# server.mappath