【问题标题】:Directory.GetFiles(Server.MapPath()) from dropdown.SelectedValueDirectory.GetFiles(Server.MapPath()) 来自 dropdown.SelectedValue
【发布时间】: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


【解决方案1】:

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

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

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

@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

【讨论】:

  • 感谢到目前为止的帮助,我进行了更改,当我从下拉列表中选择时,它会将正确的路径存储到“Model.albumPath”中。现在我无法让它将图像加载到页面上。我在“foreach”行和“var img =...”上放了一个断点,但它永远不会在“var img =...”处中断。它不会进入循环。
  • @EricF 我注意到在Directory.GetFiles 的第二个参数中你传递了" *.jpg"(注意* 之前的空格)。我怀疑您只想通过 "*.jpg" 来获取该目录中的所有 jpg 文件。我会更新我的答案以反映这一点。
  • 好收获。它现在正在进入循环,但我收到错误“...' is a physical path, but a virtual path is expected.”所以“albumPath”正在获取正确的虚拟路径,但“ImgPath”正在获取图像的物理路径。
  • Directory.GetFiles 返回的路径将是物理路径,而不是虚拟路径,因此无需在imgPath 值上调用Server.MapPath。您能否分享引发此错误的特定代码行?
  • 我在 'var img = new FileInfo(imgPath);' 上得到错误我不明白的是,我目前使用的是相同的代码,但我有一个静态位置,即“~/photos/halloween”,而不是尝试使用变量“albumPath”及其工作。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2016-03-01
    • 2013-02-24
    • 2014-01-11
    • 2013-10-11
    相关资源
    最近更新 更多