简体   繁体   English

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

[英]Display Image from Path in Asp.Net Core and Razor view?

I am trying to display a picture of an item in MVC View Razor webpage. 我正在尝试在MVC View Razor网页中显示项目的图片。

The following was not working. 以下无效。 ImageLocation only has picture number filename. ImageLocation仅具有图片编号文件名。

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

View was not working, just need to display one product image: 视图不起作用,只需要显示一个产品图像:

@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>

this solution did not work yet, as it using previous version of mvc net How to display an image from a path in asp.net MVC 4 and Razor view? 该解决方案尚不起作用,因为它使用的是以前版本的mvc net。 如何在asp.net MVC 4和Razor视图中从路径显示图像?

This is to get all images in the directory. 这是为了获取目录中的所有图像。 Of course you can tweak it for your needs. 当然,您可以根据需要对其进行调整。

I used something like this: 我用了这样的东西:

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

and to display it I would use: 并显示它,我将使用:

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

Tweak it for your needs 根据您的需要进行调整

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM