Reputation:
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?
Upvotes: 0
Views: 14891
Reputation: 36
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
Upvotes: 2