Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC3
Hi All,
 
I face some difficulties in MVC 3. I am really newbie in MVC 3. now i am developing sample ecommerce website. this is my models for displaying goods.
 
public class Product{
   public int productid {get;set;}
   public int CatID {get;set;}
   public string productname {get;set;}
   public string description {get;set;}
}
 
public class ProductPhoto{
   public int ProductPhotoID {get;set;}
   public int Productid {get;set;}
   public string Photopath {get;set;}
}
 
public class Category{
   public int CatID {get;set;}
   public string CatName {get;set;}
}
 
i want to show the products with picture depend on category in product
display page. So i build model like following.
 public partial class Genre
    {
        public int CatID { get; set; }
        public string CatName { get; set; }
        public List<Product> Products { get; set; }
    }
 
but i don't know how to connect with ProductPhoto table. Pls help me.
 
Best Rgds,
df
Posted 5-Sep-12 7:06am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try with this model using Entity Framework Code First...
 
public class Product{
   public int productid {get;set;}
   public int catID {get;set;}
   public string productname {get;set;}
   public string description {get;set;}
 
   [ForeignKey("catID")]
   public virtual Category ProductCategory
   [JsonIgnore]
   public virtual ICollection<ProductPhoto> Photos
}
 
public class ProductPhoto{
   public int ProductPhotoID {get;set;}
   public int Productid {get;set;}
   public string Photopath {get;set;}
 
   [ForeignKey("Productid")]
   public virtual Product ProductOwner
}
 
public class Category{
   public int CatID {get;set;}
   public string CatName {get;set;}
   [JsonIgnore]
   public virtual ICollection<Product> Products
}
 
Then set your DbContext using DbSet<Category> Categories, DbSet<Product> Products, DbSet<ProductPhoto> ProductPhotos. If you are not using JSON serialization you can easily ignore [JsonIgnore] attribute.
 
Use this query to retieve images from Product based on category. I am retrieving the images of the first product only.
var v = dbContext.Products.Where(p=>p.catID == suppliedCatgoryId).First();
var images = v.Photos
Let me know if this helps.
  Permalink  
Comments
dartfrog at 7-Sep-12 5:06am
   
Ok .. but now i use linq to sql classes how should i do ?
 
Genre gen = from prd in db.Products
join prdphoto in db.ProductPhotos on prd.productid equals prdphoto.productid
join cat in db.Categories on prd.CatID equals cat.CatID
where cat.CatName==categoryName
 
i don't know how to write "select" statement for Genre class.
ray_mayukh at 12-Sep-12 7:57am
   
var v = from p in dbContext.Products

where p.catID == suppliedCatgoryId

select new{
photos = p.Photos
};

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100