簡體   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