I am using MVC6 and Entity Framework 7.
Basically I have 2 main tables. I want to show table 1 in an enumerated list on the view page and then pull from table 2 while cycling through table 1.
So when I am displaying table 1's data I need to lookup table 2 data that matches the Id of the record.
Here are the 2 models. School
public class School
{
[Key]
public int SchoolId { get; set; }
[Required]
[Display(Name = "Name")]
public string SchoolName { get; set; }
}
Match
public class Match
{
[Key]
public int MatchId { get; set; }
public int SchoolId { get; set; }
public int SeasonId { get; set; }
public School School { get; set; }
public Season Season { get; set; }
}
So I need to pull from the Match table where the SchoolId matches the current school I am displaying and where the SeasonId is the current season which is why I can't link these tables up together. Maybe I am going about the data stucture setup wrong.
Here is the ViewModel I started working on I think I set it up wrong.
public class SchoolHomeViewModel
{
public IQueryable<School> SchoolData { get; set; }
public IQueryable<Match> MatchData { get; set; }
}
Here is the controller code.
public IActionResult Index()
{
var vm = new SchoolHomeViewModel();
vm.SchoolData = _context.Schools;
vm.MatchData = _context.Matches
.Where(s => s.SeasonId == 1);
return View(vm);
}
And finally the View which I can't figure out how to code properly
@model IEnumerable<SchoolHomeViewModel>
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.SchoolData)
@Html.DisplayFor(modelItem => item.MatchData)
}
This is where I am getting confused, I know the code is broken and won't run. I need to send a list of Schools and Matches. Cycle through the schools first then cycle through the Matches within the Schools to display that accordingly.