在View中显示文件夹中的图像

时间:2021-10-26 00:21:46

Im having a problem showing my images from a folder within my app. I have been looking a bit around and found this thread Display all images in a folder in MVC. With a foreach.

我有问题从我的应用程序中的文件夹显示我的图像。我一直在寻找,发现这个线程显示MVC文件夹中的所有图像。带着foreach。

I have tried to implement the suggestion with the viewmodel and my code looks like this.

我试图用viewmodel实现建议,我的代码看起来像这样。

ViewModel

public class ListBooksViewModel
{
    public IEnumerable<string> Images { get; set; }
}

Controller

public ActionResult _ListBooks()
{
    var model = new ListBooksViewModel()
    {
      Images = Directory.EnumerateFiles(Server.MapPath("~/BookImageStorage")).Select(fn =>"~/BookImageStorage" + Path.GetFileName(fn))
    };

        return PartialView(model);
    }

View

@model How.ViewModels.ListBooksViewModel
@foreach (var image in Model.Images)
{
    <img src="@Url.Content(image)" />
}

My problem is I dont get any of the picture shown, I do however get the icon for an image for every image in my folder, and no error.. Can anyone explain to me why this is happening and help me fix the issue.

我的问题是我没有得到任何显示的图片,但我确实为我的文件夹中的每个图像获取图像的图标,并且没有错误。任何人都可以向我解释为什么会发生这种情况并帮助我解决问题。

Thx and merry Christmas to those of you celebrate it..

对你们这些人来说,圣诞快乐和圣诞节庆祝它。

1 个解决方案

#1


0  

Add a slash between the folder name and the file name.( Before Path.GetFileName(fn)). This should work.

在文件夹名称和文件名之间添加斜杠。(在Path.GetFileName(fn)之前)。这应该工作。

public ActionResult _ListBooks()
{
    var model = new ListBooksViewModel()
    {
        Images = Directory.EnumerateFiles(Server.MapPath("~/BookImageStorage"))
                   .Select(fn => "~/BookImageStorage" + "/"+Path.GetFileName(fn))
    };
    return PartialView(model);
}

#1


0  

Add a slash between the folder name and the file name.( Before Path.GetFileName(fn)). This should work.

在文件夹名称和文件名之间添加斜杠。(在Path.GetFileName(fn)之前)。这应该工作。

public ActionResult _ListBooks()
{
    var model = new ListBooksViewModel()
    {
        Images = Directory.EnumerateFiles(Server.MapPath("~/BookImageStorage"))
                   .Select(fn => "~/BookImageStorage" + "/"+Path.GetFileName(fn))
    };
    return PartialView(model);
}