I’m building a web app using Entity Framework Core.
I have a view model with a property:
Public IEnumerable<AppointmentDTO> Appointments
Appointment
has a foreign key DoctorId
.
Now in the view where Im using the view model, I have:
@foreach (var doc in Model.Doctors)
{
var appointment = Model.Appointments?.FirstOrDefault(a => a.DoctorId == doc.Id);
}
The issue is all DoctorId
are returned as null
, for all appointments, even though it’s not.
Does anybody know what the issue could be?
Appointment model
public int? DoctorId { get; set; }
public Doctor Doctor { get; set; }
Doctor model
public List<Appointment>? Appointments { get; set; }
I thought it could be because doctorId
is nullable, so I made it required, but that didn’t work, either.
2
Answers
viewModel:
view:
Make sure in the controller you have define the view model , set the list Doctors and Appointments value.
like: