Прошу помочь с данной ошибкой: NullReferenceException: Object reference not set to an instance of an object.
HistoriesController.cs
public async Task<IActionResult> Index()
{
var mymodel = new ViewModel();
mymodel.Histories = await _context.Histories.Include(h => h.Doctor).Include(h => h.Patient).ToListAsync();
mymodel.Specialists = await _context.Specialists.Include(s => s.Name).ToListAsync();
return View(mymodel);
}
Index.cshtml
@foreach (Specialist specialist in Model.Specialists)
{
<td>
@Html.DisplayFor(modelItem => specialist.Name)
</td>
}
ViewModel.cs
public class ViewModel
{
public IEnumerable<Patient> Patients { get; set; }
public IEnumerable<History> Histories { get; set; }
public IEnumerable<Doctor> Doctors { get; set; }
public IEnumerable<Specialist> Specialists { get; set; }
}
.Includeкак будто это класс. Уберите.Include(s => s.Name)- ошибка пропала? Кроме того, у вас не заполняется Patients и Doctors - и если у вас в index или его дочерних представлениях идёт обращение к ним - то на них и грохнется. – A K Apr 14 '19 at 16:25