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:
- For each class that needs to create a partial view: i. in your case, create a partial view for
Match & Person ; Methods in the controller should return a partial view : return PartialView(model); (what is the difference View() & PartialView() can be found here )
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 ΡΠ°Π·ΠΌΠ΅ΡΠΊΠ°
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