I’m learning MVC 3 and i’ve got a problem with one thing.
I have two tables in my model (Gallery and Image). These tables ar related (one-to-many) by GalleryId (1 gallery – many images).
On Gallery -> Details view i would like to insert gallery details(which is simple) and one more thing – list of images from this gallery. I have no idea how to do it.
Here are classes from this model:
public partial class Gallery
{
public Gallery()
{
this.Images = new HashSet<Image>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public System.DateTime CreatedOn { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public partial class Image
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public int GalleryId { get; set; }
public System.DateTime UploadedOn { get; set; }
public virtual Gallery Gallery { get; set; }
}
public partial class MyEntities : DbContext
{
public DbSet<Gallery> Galleries { get; set; }
public DbSet<Image> Images { get; set; }
}
What is the best way to do it?
In the detailview, you use the Gallery as your model (in the controller you return the desired gallery
return ActionResult View(service.GetGallery(id));and in the view you have a loop: