public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Patient patient = db.Patients.Find(id); if (patient == null) { return HttpNotFound(); } var completeAttributes = new List<CompleteAttribute>(); foreach (var i in db.PatientAttributes.ToList()) { var patientAttribute = db.Patient_PatientAttributes.SingleOrDefault(j => j.PatientAttributeID == i.ID && j.PatientID == patient.ID); PatientAttributeValue selectedAttributeValue = null; if (patientAttribute != null) selectedAttributeValue = db.PatientAttributeValues.Find(patientAttribute.PatientAttributeValueID); else selectedAttributeValue = new PatientAttributeValue(); var completeAttribute = new CompleteAttribute { PatientAttribute = i, PatientAttributeValues = i.PatientAttributeValues, SelectedPatientAttributeValue = selectedAttributeValue }; completeAttributes.Add(completeAttribute); } var patient_PatientAttributesList = db.Patient_PatientAttributes.Where(i => i.PatientID == patient.ID).ToList(); var patientVM = new PatientViewModel() { Patient = patient, CompleteAttributes = completeAttributes }; return View(patientVM); }
// GET: Patients/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Patient patient = db.Patients.Find(id); if (patient == null) { return HttpNotFound(); } var completeAttributes = new List<CompleteAttribute>(); var patientsAttributes = db.Patient_PatientAttributes.Where(i => i.PatientID == patient.ID).ToList(); var patientAttributes = db.PatientAttributes.ToList(); foreach(var a in patientAttributes) { var attributeValues = a.PatientAttributeValues; var selectedAttribute = db.Patient_PatientAttributes.SingleOrDefault(i => i.PatientAttributeID == a.ID && i.PatientID == patient.ID); PatientAttributeValue selectedAttributeValue = null; if (selectedAttribute != null) { selectedAttributeValue = selectedAttribute.PatientAttributeValue; } else { selectedAttributeValue = new PatientAttributeValue { ID = 0, Value = "Please Select Attribute", PatientAttributeID = a.ID }; attributeValues.Add(new PatientAttributeValue { Value = "Please Select Attribute", ID = 0, PatientAttributeID = a.ID }); } var completeAttribute = new CompleteAttribute { PatientAttribute = a, PatientAttributeValues = attributeValues, SelectedPatientAttributeValue = selectedAttributeValue }; completeAttributes.Add(completeAttribute); } var patientVM = new PatientViewModel { Patient = patient, CompleteAttributes = completeAttributes }; return View(patientVM); }
public ActionResult Create(PatientViewModel patientVM) { var patient = new Patient { FirstName = patientVM.Patient.FirstName, LastName = patientVM.Patient.LastName }; if (ModelState.IsValid) { db.Patients.Add(patient); db.SaveChanges(); } using (var db = this.db) { using (var transaction = db.Database.BeginTransaction()) //this allows you to have two savechanges() in one function { try { var patient_PatientAttributesList = new List<Patient_PatientAttribute>(); foreach (var ca in patientVM.CompleteAttributes) { Patient_PatientAttribute patientAttribute = null; if (ca.PatientAttribute.Numeric) { //for numeric attributes, the attribute value assigned to the patient is contained in the AttributeValue table, and then also referenced //in the Patient_PatientAttributes table var attributeValue = new PatientAttributeValue { PatientAttributeID = ca.PatientAttribute.ID, Value = ca.SelectedPatientAttributeValue.Value }; db.PatientAttributeValues.Add(attributeValue); db.SaveChanges(); patientAttribute = new Patient_PatientAttribute { PatientAttributeID = ca.PatientAttribute.ID, PatientAttributeValueID = attributeValue.ID, PatientID = patient.ID }; } else { patientAttribute = new Patient_PatientAttribute { PatientAttributeID = ca.PatientAttribute.ID, PatientAttributeValueID = ca.SelectedPatientAttributeValue.ID, PatientID = patient.ID }; } patient_PatientAttributesList.Add(patientAttribute); } if (ModelState.IsValid) { foreach (var pa in patient_PatientAttributesList) { db.Patient_PatientAttributes.Add(pa); } //db.Patient_PatientAttributes.AddRange(patientsAttributesList); db.SaveChanges(); transaction.Commit(); } } catch (Exception) { transaction.Rollback(); } } } return RedirectToAction("Index"); }