I need to display data from two database tables on one page. So far I can only get data from one table.

Here is what it is:

Model1:

public class Match { public int Id { get; set; } public string Team1 { get; set; } public string Team2 { get; set; } public int Rate { get; set; } public int BO { get; set; } } 

Model2:

 public class Person { public int Id { get; set; } public string Name { get; set; } public int Days { get; set; } public DateTime Date { get; set; } } 

Data context:

 public class MatchContext : DbContext { public DbSet<Match> Matches { get; set; } public DbSet<Person> Persons { get; set; } } 

Controller:

 public class NeuroController : Controller { MatchContext db = new MatchContext(); public ActionResult Index() { return View(db); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } 

And in the presentation:

 @model IEnumerable<NB.Models.Match> @{ ... 

How to display the models "Match" and "Person"?

  • @Bald I created such a model, then in the controller I added MatchPersonViewModel db = new MatchPersonViewModel(); public ActionResult Index() { return View(db); } MatchPersonViewModel db = new MatchPersonViewModel(); public ActionResult Index() { return View(db); } MatchPersonViewModel db = new MatchPersonViewModel(); public ActionResult Index() { return View(db); } And in the @model IEnumerable<NB.Models.MatchPersonViewModel> But everything just fails to refer to the table - Pavel Blokhin
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
  • Added a link to an example for ViewModel - Bald

2 answers 2

A little more detail about the methods proposed by @DreamChild

Create a model that will contain the necessary data for the presentation:

 public class ViewModel { public IEnumerable<Match> Matches {get;set;} public IEnumerable<Person> Persons {get;set;} //ΠΈΠ»ΠΈ ΠΆΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½Ρ‹Π΅ свойства ΠΈΠ· классов } 

in the method of the caller, this view must obtain the necessary data and create an instance of the ViewModel to fill it in and give it to the view

 public class NeuroController : Controller { public ActionResult GetViewModel() { var db = new MatchContext(); var matches = db.Matches.ToList(); var persons = db.Persons.ToList(); var model = new ViewModel { Matches = matches, Persons = persons}; return View(model); } } 

as a model in the view, you must specify the ViewModel we created: @model ViewModel

Matches | Persons in the view will look like: @Model. i.e. @Model.Matches | @Model.Persons

Example


The variant with partial views is implemented as follows:

  1. For each class that needs to create a partial view: i. in your case, create a partial view for Match & Person ;
  2. Methods in the controller should return a partial view : return PartialView(model); (what is the difference View() & PartialView() can be found here )

  3. In the MatchPerson view, you will need to call the @Html.RenderAction method which will return a partial view ;


An example of a partial view for listing the Person list:

 @model IEnumerable<Person> <table class="table"> <tr> <th>@Html.DisplayNameFor(model => model.Name)</th> <th>@Html.DisplayNameFor(model => model.Days)</th> <th>@Html.DisplayNameFor(model => model.Date)</th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.Name)</td> <td>@Html.DisplayFor(modelItem => item.Days)</td> <td>@Html.DisplayFor(modelItem => item.Date)</td> </tr> } </table> 

Example of controller method

 public ActionResult GetPersons() { var persons = db.Persons.ToList(); return PartialView(persons); } 

somewhere in the view to display two models

 //html Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΠ° @Html.RenderActions("GetPersons") //html Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΠ° 
  • In the first case, when I try to specify a model in the @model ViewModel view, I @model ViewModel that I could not find the type or namespace of the ViewModel. (Did the same as you wrote) - Pavel Blokhin
  • @PavelBlokhin add the ViewModel you created to the question - Bald
  • The same as uvas. public class ViewModel { public IEnumerable<Match> Matches {get;set;} public IEnumerable<Person> Persons {get;set;} } - Pavel Blokhin
  • @PavelBlokhin show the controller method where you fill in the model and give it to the view - Bald
  • This is what the MatchContext db = new MatchContext(); public ActionResult Index() { var matches = db.Matches.ToList(); var persons = db.Persons.ToList(); var model = new ViewModel { Matches = matches, Persons = persons }; return View(model); } controller looks like MatchContext db = new MatchContext(); public ActionResult Index() { var matches = db.Matches.ToList(); var persons = db.Persons.ToList(); var model = new ViewModel { Matches = matches, Persons = persons }; return View(model); } MatchContext db = new MatchContext(); public ActionResult Index() { var matches = db.Matches.ToList(); var persons = db.Persons.ToList(); var model = new ViewModel { Matches = matches, Persons = persons }; return View(model); } MatchContext db = new MatchContext(); public ActionResult Index() { var matches = db.Matches.ToList(); var persons = db.Persons.ToList(); var model = new ViewModel { Matches = matches, Persons = persons }; return View(model); } - Pavel Blokhin

Either have your page consist of two separate partial views, each of which will have a model of the corresponding class, or, if this option is not applicable, create one uber model that includes instances of both classes (or the required fields from both classes)

  • What will this uber model look like? - Pavel Blokhin
  • one
    @PavelBlokhin as a class with two properties. Or even just fields. - Pavel Mayorov