Reputation: 418
I have below to fetch (retrieve) images from database by id,(all these images has an ID).
<img src="@Url.Action("GetMainPicture", "Product", new { item.ProductID, @class = "img-responsive" })" alt="" />
Then i have an <a href>
tag which calls for Popup and inside the popup i used the same line of code which is used for fetching the image, this here:
<img src="@Url.Action("GetMainPicture", "Product", new { item.ProductID, @class = "img-responsive" })" alt="" />
The above line of code retrieves the picture but the problem is that it fetches the image with id = 1, for every picture. To give you in depth information i paste the whole code below:
@model List<Hosay.Domain.Entities.Product>
@foreach (var item in Model)
{
<!-- PRODUCT ITEM START -->
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="product-item">
<div class="pi-img-wrapper">
<img src="@Url.Action("GetMainPicture", "Product", new { item.ProductID, @class = "img-responsive" })" height="300" width="230" alt="" />
<div>
<a href="../../assets/frontend/pages/img/products/model1.jpg" class="btn btn-default fancybox-button">Zoom</a>
<a href="#product-pop-up" class="btn btn-default fancybox-fast-view">View</a>
</div>
</div>
<h3>@Html.ActionLink(item.ProductName, "ProductDetails", new { item.ProductID })</h3>
<div class="pi-price">@item.ProductPrice.ToString("c")</div>
<a href="javascript:;" class="btn btn-default add2cart">Add to cart</a>
</div>
</div>
<!-- PRODUCT ITEM END -->
<!--Product Quick View Start-->
<div id="product-pop-up" style="display: none; width: 700px;">
<div class="product-page product-pop-up">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-3">
<div class="product-main-image">
<img src="@Url.Action("GetMainPicture", "Product", new { item.ProductID })" class="img-responsive" alt="" />
</div>
<div class="product-other-images">
<a href="javascript:;" class="active"><img alt="Berry Lace Dress" src="~/Images/Products/model3.jpg"></a>
<a href="javascript:;"><img alt="Berry Lace Dress" src="~/Images/Products/model4.jpg"></a>
<a href="javascript:;"><img alt="Berry Lace Dress" src="~/Images/Products/model5.jpg"></a>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-9">
<h2>Cool green dress with red bell</h2>
<div class="price-availability-block clearfix">
<div class="price">
<strong><span>$</span>47.00</strong>
<em>$<span>62.00</span></em>
</div>
<div class="availability">
Availability: <strong>In Stock</strong>
</div>
</div>
<div class="description">
<p>
Lorem ipsum dolor ut sit ame dolore adipiscing elit, sed nonumy nibh sed euismod laoreet dolore magna aliquarm erat volutpat
Nostrud duis molestie at dolore.
</p>
</div>
<div class="product-page-options">
<div class="pull-left">
<label class="control-label">Size:</label>
<select class="form-control input-sm">
<option>L</option>
<option>M</option>
<option>XL</option>
</select>
</div>
<div class="pull-left">
<label class="control-label">Color:</label>
<select class="form-control input-sm">
<option>Red</option>
<option>Blue</option>
<option>Black</option>
</select>
</div>
</div>
<div class="product-page-cart">
<div class="product-quantity">
<input id="product-quantity" type="text" value="1" readonly name="product-quantity" class="form-control input-sm">
</div>
<button class="btn btn-primary" type="submit">Add to cart</button>
<a href="shop-item.html" class="btn btn-default">More details</a>
</div>
</div>
<div class="sticker sticker-sale"></div>
</div>
</div>
</div>
<!--Product Quick View End-->
}
What is the problem, i couldn't found anything online so i asked it here.
EDIT:
public FileContentResult GetMainPicture(int productId)
{
Product prod = repository.Products
.FirstOrDefault(p => p.ProductID == productId);
if (prod != null)
{
return File(prod.MainPicture, prod.MainPictureMimeType);
}
else
{
return null;
}
}
Naser
Upvotes: 0
Views: 61
Reputation: 24147
Inside the for
-loop you create multiple divs with the same id: <div id="product-pop-up"
, which is not allowed in HTML/DOM. ID values must be unique on the page. This is probably the cause of your problem.
Make the IDs unique, and then adjust the part that causes them to show in the same way.
EDIT - here's how:
Change @for loop to start like this:
@{
var imgCounter = 0;
}
@foreach (var item in Model)
{
imgCounter++;
Then change the <a href>
to look like this:
<a href="#product-pop-up-@(imgCounter)" class="btn btn-default fancybox-fast-view">View</a>
And change the <div>
to start like this:
<div id="product-pop-up-@(imgCounter)" style="display: none; width: 700px;">
Here I am assuming that the #
-value in the href
is what causes the div to show.
Upvotes: 1