来自dropdown.SelectedValue的Directory.GetFiles(Server.MapPath())

时间:2017-11-03 18:39:51

标签: c# server.mappath

我有一个照片库,在不同的文件夹中有不同的设置。目前我的每个页面都有不同的页面。我想要做的是使用下拉列表选择要显示的集合。我正在使用

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>
&#13;
&#13;
&#13; 我试图做像

这样的事情
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中)不存在的错误。我试过在模型和控制器中声明它们。有没有办法让这个工作或有更好的方法来做到这一点?

下面是我目前正在努力工作的视图,控制器和模型

查看

&#13;
&#13;
@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>
&#13;
&#13;
&#13;

控制器

&#13;
&#13;
        public ActionResult Gallery(string Album, string albumPath)
        {
            //albumPath = ("~/photos/" + Album);
            return View();



        }
&#13;
&#13;
&#13;

和模型

&#13;
&#13;
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; }

    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

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

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

接下来,您将视图的模型定义为IEnumerable的{​​{1}}

GalleryModel

这使视图期望一组对象。在您的情况下,您似乎只想在视图中显示一个图库,因此您的@model定义应该如下所示。

@model IEnumerable<WebsiteMVC.Models.GalleryModel>

现在,您可以从视图中访问@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

答案 1 :(得分:1)

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