0

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.

2
  • Your view is expecting an IEnumerable of SchoolHomeViewModel but you are passing it only a single SchoolHomeViewModel object. Commented Jan 21, 2016 at 16:17
  • This is where I am getting confused, I need to send a list of Schools and Matches. Cycle through the schools first then cycle through the Matches withing the Schools to display that accordingly. Commented Jan 21, 2016 at 16:19

1 Answer 1

1

First let's get your view to accept the correct ModelType, a single ScholHomeViewModel instead of an IEnumerable. This is the model declaration at the top of the View.

@model PTPWebApp.ViewModels.Schools.SchoolHomeViewModel

Since you no longer have your model declared as a list, no sense in doing the foreach loop, but instead SchoolData and MatchData are lists themselves. So let's rework what we are doing in your loop:

<table>
<th><td>School ID</td><td>School Name</td></th>
    @foreach (var school in item.SchoolData)
    {
       @<tr>
            <td>@school.SchoolId</td>
            <td>@school.SchoolName</td>
       </tr>
    }
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Ok thank you so much this got the school info to start showing up. Now for the Match data. Do I open another foreach within there and check if the schoolids match then display? I tried but it throws an error saying I already have an open DataReader.
Nevermind I added MultipleActiveResultSets=true to my connection string and it worked. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.