public ActionResult SubmitSelectedPhysicianList(int thePatientId, PhysicianSelectionViewModel viewModel) { Patient patient = _context.Patients.Find(thePatientId); //Patient patient = viewModel.thePatient; //var thePatientId = viewModel.getPatientId(); //get the ids from items selected var selectedPhysicianIds = viewModel.getSelectedPhysicianIds(); //Use the ids to retrieve the records for the selected physicians //from database var selectedPhysicians = (from x in _context.Physicians where selectedPhysicianIds.Contains(x.Id) select x).ToList(); //add each selected physician to the patient foreach (var physician in selectedPhysicians) { patient.Physicians.Add(physician); //System.Diagnostics.Debug.WriteLine(physician.PhysicianName, physician.PhysicianSpecialty); // _context.SaveChanges(); } _context.SaveChanges(); //If everything is ok, redirect back to patient detail if (ModelState.IsValid) { Session["PhysicianSelectionViewModel"] = viewModel; // var patientID = viewModel.Patient.Id; return(RedirectToAction("Details", "Patients", new { patientId = thePatientId })); } //If something goes wrong, keep user at the same page //return View("PhysicianListForSelection", viewModel); return(RedirectToAction("Details", "Patients", new { patientId = thePatientId })); }
//Action method to display selection of physicians public ActionResult GetPhysicianList(int patientId) { var viewModel = new PhysicianSelectionViewModel(); foreach (var physician in _context.Physicians) { var editorViewModel = new SelectPhysicianEditorViewModel() { Selected = false, Name = physician.PhysicianName, Specialty = physician.PhysicianSpecialty, Address = physician.PhysicianAddress, physicianId = physician.Id, patientId = patientId }; viewModel.PhysicianList.Add(editorViewModel); } viewModel.thePatient = _context.Patients.Find(patientId); return(View("PhysicianListForSelection", viewModel)); }