I read similar questions about SO, but I can not understand this problem, which is specific to DBContext objects (I think). Here is some dummy code to illustrate.
I have the following code in an Index () action:
public ActionResult Index() { AnimalDBContext db = new AnimalDBContext(); return View(db.Dogs); }
I have the following code for my models:
public class Dog { public int ID { get; set; } public string name { get; set; } public string breed { get; set; } } public class AnimalDBContext : DbContext { public DbSet<Dog> Dogs { get; set; } }
In my opinion, I have the following:
@model IEnumerable<AnimalProject.Models.Dog> @foreach (var d in Model) { <h3>@d.name</h3> <h2>@d.breed</h2> }
Everything works fine, the view will go through every dog ββin my database. However, I have another DBContext dataset from another table that I want in the same view. I want to be able to list each item in the database for this table.
This is what I want if you catch my drift:
@model IEnumerable<AnimalProject.Models.Dog> @model IEnumerable<AnimalProject.Models.Cat> @foreach (var d in Dog) { <h3>@d.name</h3> <h2>@d.breed</h2> } @foreach (var c in Cat) { <h3>@c.name</h3> <h2>@c.breed</h2> }
I tried to group classes together and use a partial view, but apparently you cannot have another model in a partial view, because I always get an error message:
"The model element passed to the dictionary is of type 'System.Data.Entity.DbSet 1[AnimalProject.Models.Dog]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [AnimalProject.Models. Cat] '".
So, how can I use several models in my view so that both get the data that I want from individual tables in the database?