繁体   English   中英

来自下拉列表的Directory.GetFiles(Server.MapPath())。SelectedValue

[英]Directory.GetFiles(Server.MapPath()) from dropdown.SelectedValue

我有一个相册,在不同的文件夹中有不同的设置。 目前我每套都有不同的页面。 我想要做的是使用下拉菜单选择要显示的设置。 我正在使用

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> 
我正在尝试做类似的事情

string albumPath = ("~/photos/" + album.selectedValue);
                    foreach (var imgPath in Directory.GetFiles(Server.MapPath(albumPath), " *.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; } } } 

首先,您没有将模型对象返回到控制器内部的视图。 您需要实例化模型类,设置其属性,然后将其传递给View()方法。

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

接下来,您将视图的模型定义为GalleryModelIEnumerable

@model IEnumerable<WebsiteMVC.Models.GalleryModel>

这使得视图期望对象的集合。 就您而言,您似乎只希望在视图中显示一个图库,因此@model定义应如下所示。

@model WebsiteMVC.Models.GalleryModel

现在,您可以从视图访问GalleryModel属性,因此可以将albumPath从模型传递到Server.MapPath

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

注意使用Model.albumPath访问模型上的albumPath属性。

最后,您给出了以下两个无效的示例:

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"))

在这两种方法中,您都尝试使用album变量,该变量尚未在任何地方定义。 如果要在模型上使用Album属性,则需要使用Model.Album

“这里是使用虚拟路径显示到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM