[英]Directory.GetFiles(Server.MapPath()) from dropdown.SelectedValue
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>
string albumPath = ("~/photos/" + album.selectedValue);
foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.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. 我不断收到这样的错误,即变量(在MapPath中)在当前上下文中不存在。 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.
您需要实例化模型类,设置其属性,然后将其传递给View()方法。
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
接下来,您将视图的模型定义为
GalleryModel
的IEnumerable
@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定义应如下所示。
@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
现在,您可以从视图访问
GalleryModel
属性,因此可以将albumPath
从模型传递到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. 注意使用
Model.albumPath
访问模型上的albumPath
属性。
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. 在这两种方法中,您都尝试使用
album
变量,该变量尚未在任何地方定义。 If you meant to use the Album
property on your model, you'd need to use Model.Album
. 如果要在模型上使用
Album
属性,则需要使用Model.Album
。
'HERE IS THE SAMPLE OF DISPLAYING TO GRIDVIEW USING VIRTUAL PATH apply it inside the button or using sub procedures “这里是使用虚拟路径显示到GRIDVIEW的示例”将其应用到按钮内部或使用子过程
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.