简体   繁体   中英

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.

The following was not working. ImageLocation only has picture number filename.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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