i am trying to display products with pictures but i've got a problem to display images,i think there is a lot i don't understand
- my first step is to display the media of each product as the table has got already a Foreign Key MediaID ,i think my model is quite fine for this step
- the second step is to make a ProductMedia table or association in order to allow a product to have several pictures,the primary key would be composed by productId and mediaId
i am lost in the middle of those 2 steps ,normally the way my view is written i expected to be able to display at the least the default pictures ,the first one in the list of media that every product must have
when i put a breakpoint in my home controller i can see that every product has a mediaId but i don't know where and how to add pictures to the list of medium so without surprise the list is empty and there is no image in any of my product
my HomeController
public ActionResult Index(string searchString)
{
var products = from p in dc.Products
select p;
....
return View(products);
}
this is my HomeIndex
@foreach (var p in Model)
{
<div class="col-lg-4 col-sm-4 hero-feature text-center">
<div class="thumbnail">
<a href="@Url.Action("Details", "Product", new { id = p.ProductID })" class="link-p" style="overflow: hidden; position: relative;">
**@if (p.Medium.Count > 0)
{
<img src="@p.Medium.ElementAt(0).MediaUrl" alt="" style="position: absolute; width: auto; height: 257px; max-width: none; max-height: none; left: 1px; top: 0px;">
}**
</a>
<div class="caption prod-caption">
<h4><a href="@Url.Action("Details", "Product", new { id = p.ProductID })">@p.ProductName.Substring(0, 1).ToUpper()@p.ProductName.Substring(1).ToLower()</a></h4>
<p>@p.ProductDescription</p>
<p>
</p>
<div class="btn-group">
<a href="#" class="btn btn-default">@p.Prices.OrderBy(pr => pr.PriceDate).FirstOrDefault().PriceValue</a>
@* <a href="#" class="btn btn-primary"><i class="fa fa-shopping-cart"></i>Buy</a>*@
@Html.ActionLink("Buy!", "Create", "Cart", new { id = p.ProductID, quantity = 1 }, new { @class = "btn btn-primary" })
</div>
<p></p>
</div>
</div>
</div>
}
the medium is a list of medium defined in the model of product each product has several media it looks like this
public partial class Product
{
public Product()
{
....
this.Medium = new HashSet<Medium>();
}
public int ProductID { get; set; }
....
public int MediaID { get; set; }
....
public virtual ICollection <Medium> Medium { get; set; }
}