The database involved includes two entities that are ID
of type int & ImagePath
of type text. The ID
holds an auto incremented number and the ImagePath
holds the URL of a specific image. Currently I've managed to display all images found in the database with the below code.
Index.cshtml
@foreach (var item in Model)
{@Html.DisplayFor(modelItem => item.ImagePath)
<img src="~/Images/@Html.DisplayFor(modelItem => item.ImagePath)" />
ImageController.cs
public class ImageController : Controller
{
private ImageDBContext db = new ImageDBContext();
public ActionResult Index()
{
return View(db.Images.ToList());
}
I would like to display a specific image and not all the images found in the database. Can someone kindly help me out in displaying a specific URL stored image from the database by ID
? (ie. Display Image from ImagePath
through a called ID in the .cshtml)
Highly appreciate your time and help. Thank you.
UPDATE:
Wrote the below code down in order to Display image from db.image
table having chosen ImageID
= 2 to represent the related url stored image.
ImageController.cs
public ActionResult Index(int ImageID = 2)
{
return View(db.Images.Where(img => img.ID == ImageID).ToList());
}
Index.cshtml
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.ImagePath)
<img src="~/Images/@Html.DisplayFor(modelItem => item.ImagePath)"/>
Moving forward I would like to select the ImageID
from the Index.cshtml instead of int ImageID = 2
from the public ActionResult Index()
in the controller. Was thinking of adding ImageID == 2
in the @html.DisplayFor
but don't know how. Any ideas how to select the ImageID
from the index.cshtml code to represent the image related?*
END GOAL
Having the below <img src=""/>
replaced to call imagePath
from the given ID
.
<a href="https://www.facebook.com/vellanevets" target="_blank"> <img src="images/social/instagram.png" /></a>
<a href="http://instagram.com/nevetsvella/" target="_blank"> <img src="images/social/instagram.png" alt="" /></a>
<a href="http://vimeo.com/user16587212" target="_blank"> <img src="images/social/vimeo.png" alt="" /></a>
<a href="mailto:steven.vella93@gmail.com" target="_blank"> <img src="images/social/email.png" alt="" /></a>
}