在Asp.Net Core和Razor视图中显示来自Path的图像?

问题描述 投票:0回答:1

我想在MVC View Razor网页上显示一个项目的图片。

以下不起作用。 ImageLocation只有图片编号文件名。

Example: 
Product1.jpg
Product2.jpg
Product3.jpg
Product4.jpg

查看无法正常工作,只需要显示一个产品图片:

@model ElectronicsStore.Models.Product

@{
    ViewData["Title"] = "Details";
}

<h2>Details</h2>

<div>
    <h4>Product</h4>
    <hr />
    <dl class="dl-horizontal">

        <dt>
            <img src="~/images/@Url.Content(Model.ImageLocation)" alt="Image">
        </dt>
        <dd>

        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.ProductDescription)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.ProductDescription)
        </dd>
    </dl>
</div>

这个解决方案还没有用,因为它使用的是以前版本的mvc net How to display an image from a path in asp.net MVC 4 and Razor view?

c# razor asp.net-core asp.net-core-mvc asp.net-core-2.0
1个回答
3
投票

这是为了获取目录中的所有图像。当然,您可以根据自己的需要进行调整。

我使用过这样的东西:

var images = Directory.EnumerateFiles(Server.MapPath("Path"))
                             .Select(fn => "Path" + Path.GetFileName(fn));

并显示它我会使用:

foreach (var image in (IEnumerable<string>)images)
{
      //Your logic here eg:
      <img class="img" src="@Url.Content(image)" />
}

根据您的需要调整它

© www.soinside.com 2019 - 2024. All rights reserved.