public ActionResult Edit(int?id) { PatientDoctorsVM patientDoctorsVM = new PatientDoctorsVM(); if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } patientDoctorsVM.Patient = db.Patients.Find(id); if (patientDoctorsVM.Patient == null) { return(HttpNotFound()); } patientDoctorsVM.AllDoctors = db.Doctors.ToList(); var assignedDoctors = db.PatientDoctors.Where(p => p.Patient_Id == id); for (int i = 0; i < patientDoctorsVM.AllDoctors.Count; i++) { foreach (var IsAssigned in assignedDoctors) { if (patientDoctorsVM.AllDoctors[i].Id == IsAssigned.Doctor_Id) { patientDoctorsVM.AllDoctors[i].IsSelected = true; } } } return(View(patientDoctorsVM)); }
public ActionResult Edit(PatientDoctorsVM patientDocs) { if (ModelState.IsValid) { Patient patient = patientDocs.Patient; db.Entry(patient).State = EntityState.Modified; db.SaveChanges(); PatientDoctors doctorAssigned = new PatientDoctors(); doctorAssigned.Patient_Id = patientDocs.Patient.Id; var assignedDoctors = db.PatientDoctors.Where(p => p.Patient_Id == patient.Id).ToList(); foreach (var doctor in patientDocs.AllDoctors.Where(s => s.IsSelected)) { doctorAssigned.Doctor_Id = doctor.Id; db.PatientDoctors.Add(doctorAssigned); db.SaveChanges(); } } return(RedirectToAction("Index")); }