Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ActionResult Index()
- {
- AnimalDBContext db = new AnimalDBContext();
- return View(db.Dogs);
- }
- 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; }
- }
- @model IEnumerable<AnimalProject.Models.Dog>
- @foreach (var d in Model)
- {
- <h3>@d.name</h3>
- <h2>@d.breed</h2>
- }
- @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>
- }
- public AnimalModel
- {
- public IEnumerable<Dog> Dogs { get; set; }
- public IEnumerable<Cat> Cats { get; set; }
- }
- public ActionResult Index()
- {
- using (var db = new AnimalDBContext())
- {
- var model = new AnimalModel
- {
- Dogs = db.Dogs.ToList(),
- Cats = db.Cats.ToList()
- };
- return View(model);
- }
- }
- @model AnimalProject.Models.AnimalModel
- @foreach (var d in Model.Dogs)
- {
- <h3>@d.name</h3>
- <h2>@d.breed</h2>
- }
- @foreach (var c in Model.Cats)
- {
- <h3>@c.name</h3>
- <h2>@c.breed</h2>
- }
- public ActionResult Index()
- {
- return View(new AnimalDBContext()); // We pass a new instance of the dbcontext to the view
- }
- @model AnimalProject.AnimalDBContext // we declare that our model is of type AnimalDBContext
- @foreach (var d in Model.Dogs) // We reference the dbcontext's sets directly
- {
- <h3>@d.name</h3>
- <h2>@d.breed</h2>
- }
- @foreach (var c in Model.Cats) // We reference the dbcontext's sets directly
- {
- <h3>@c.name</h3>
- <h2>@c.breed</h2>
- }
- ViewBag.Dogs = db.Dogs;
- ViewBag.Cats = db.Cats;
- @Html.Partial("_ListDogs", ViewBag.Dogs)
- @Html.Partial("_ListCats", ViewBag.Cats)
- @model IEnumerable<AnimalProject.Models.Dog>
- @foreach (var d in Model)
- {
- <h3>@d.name</h3>
- <h2>@d.breed</h2>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement