0

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; }

        }
1
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. Commented Feb 25, 2017 at 2:32

1 Answer 1

1

You don't need to define ProductMedia table since your relation seems to be 1-many.

Third table is defined in many-many relationships. each Product has multiple Media and each Media belongs to a single Product.

Your model does not need MediaId, which I think is the id for your default picture.

What you need to do is to pull media related to each product inside your controller.Something like this:

public ActionResult Index(string searchString)
{
   var products = dc.Products.ToList();

   foreach(p in products)
   {
       //I assume that your Medium table has a column named ProductID which is a foreign key to Product table
       p.Medium = dc.Medium.Where(x => x.ProductID == p.ProductID).ToList();
   }
    return View(products);
}

Inside your view, change the commented if statement to this in order to show the default Media of your product:

@if (p.Medium != Null && p.Medium.Any())
 {
      <img src="@p.Medium.First().MediaUrl" alt="" style="position: absolute; width: auto; height: 257px; max-width: none; max-height: none; left: 1px; top: 0px;">
 }
Sign up to request clarification or add additional context in comments.

8 Comments

So, You cant relate Media to Product. In 1-many relations, the dependent entity must have a foreign key of an independent entity. In your scenario, Product is independent.In other words, Media is meaningless without Product, but Product is standalone entity, no matter it has Media or not. Add ProductId to Media table. If you are using Media for other entities like News or Users, you should add NewId or UserId to the Media table.
Since messing up controller code with queries is a bad idea, I recommend you to use Entity Framework AutoMapper for your future projects
i correct it but cannot implicitly convert type system linq iqueryable to system collections generic Icollection this is the error i got on the loop
I edited the answer. I used straightforward LINQ statement
The specified type member 'ProductID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.