Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public ActionResult Index()
  2. {
  3. AnimalDBContext db = new AnimalDBContext();
  4.  
  5. return View(db.Dogs);
  6. }
  7.  
  8. public class Dog
  9. {
  10. public int ID { get; set; }
  11. public string name { get; set; }
  12. public string breed { get; set; }
  13. }
  14.  
  15. public class AnimalDBContext : DbContext
  16. {
  17. public DbSet<Dog> Dogs { get; set; }
  18. }
  19.  
  20. @model IEnumerable<AnimalProject.Models.Dog>
  21. @foreach (var d in Model)
  22. {
  23. <h3>@d.name</h3>
  24. <h2>@d.breed</h2>
  25. }
  26.  
  27. @model IEnumerable<AnimalProject.Models.Dog>
  28. @model IEnumerable<AnimalProject.Models.Cat>
  29. @foreach (var d in Dog)
  30. {
  31. <h3>@d.name</h3>
  32. <h2>@d.breed</h2>
  33. }
  34. @foreach (var c in Cat)
  35. {
  36. <h3>@c.name</h3>
  37. <h2>@c.breed</h2>
  38. }
  39.  
  40. public AnimalModel
  41. {
  42. public IEnumerable<Dog> Dogs { get; set; }
  43. public IEnumerable<Cat> Cats { get; set; }
  44. }
  45.  
  46. public ActionResult Index()
  47. {
  48. using (var db = new AnimalDBContext())
  49. {
  50. var model = new AnimalModel
  51. {
  52. Dogs = db.Dogs.ToList(),
  53. Cats = db.Cats.ToList()
  54. };
  55.  
  56. return View(model);
  57. }
  58. }
  59.  
  60. @model AnimalProject.Models.AnimalModel
  61. @foreach (var d in Model.Dogs)
  62. {
  63. <h3>@d.name</h3>
  64. <h2>@d.breed</h2>
  65. }
  66. @foreach (var c in Model.Cats)
  67. {
  68. <h3>@c.name</h3>
  69. <h2>@c.breed</h2>
  70. }
  71.  
  72. public ActionResult Index()
  73. {
  74. return View(new AnimalDBContext()); // We pass a new instance of the dbcontext to the view
  75. }
  76.  
  77. @model AnimalProject.AnimalDBContext // we declare that our model is of type AnimalDBContext
  78. @foreach (var d in Model.Dogs) // We reference the dbcontext's sets directly
  79. {
  80. <h3>@d.name</h3>
  81. <h2>@d.breed</h2>
  82. }
  83. @foreach (var c in Model.Cats) // We reference the dbcontext's sets directly
  84. {
  85. <h3>@c.name</h3>
  86. <h2>@c.breed</h2>
  87. }
  88.  
  89. ViewBag.Dogs = db.Dogs;
  90. ViewBag.Cats = db.Cats;
  91.  
  92. @Html.Partial("_ListDogs", ViewBag.Dogs)
  93. @Html.Partial("_ListCats", ViewBag.Cats)
  94.  
  95. @model IEnumerable<AnimalProject.Models.Dog>
  96. @foreach (var d in Model)
  97. {
  98. <h3>@d.name</h3>
  99. <h2>@d.breed</h2>
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement