I have generated a strongly-type view from a model class that I have created from scratch. I have used the List as the scaffold template. The error appears at the foreach line with the model object.
View:
@model IEnumerable<HockeyPoolStats.Models.Player>
@{
ViewBag.Title = "Index";
}
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Team_Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rank)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerTeam)
</td>
[...]
Controller:
public class PlayerController : Controller
{
private HockeyPoolStatsDB _db = new HockeyPoolStatsDB();
//
// GET: /Player/
public ActionResult Index()
{
Player _player = new Player();
_db.Players.Add(_player);
var model = _db.Players;
return View(model);
}
}
DbContext
public class HockeyPoolStatsDB : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<Goalie> Goalies { get; set; }
}
I don’t understand why model is null. This means that the controller isn’t passing the model to the view correctly?

It more likely means that
_db.Playersis returning null.Set a breakpoint and step through it and verify that
_db.Playersis actually returning a list of players.