public ActionResult AddAppointment(int pid, string cancelRedirectUrl) { ViewBag.MenuItem = CurrentMenuItem; TempData["cancelRedirectUrl"] = cancelRedirectUrl; Patient patient = pid > 0 ? unitOfWork.Repository <Patient>().Get(pid) : null; AppointmentAddModel appointmentAddModel = null; string patientName = ""; if (patient != null) { patientName = patient.FullName; appointmentAddModel = new AppointmentAddModel { PatientId = patient.Id, Reason = "", AppointmentDate = DateTime.Today }; } ViewBag.PatientName = patientName; ViewBag.CancelRedirectUrl = cancelRedirectUrl; return(View(appointmentAddModel)); }
public ActionResult AddAppointment(AppointmentAddModel model) { ViewBag.MenuItem = CurrentMenuItem; var cancelRedirectUrl = (TempData["cancelRedirectUrl"] ?? string.Empty).ToString(); ViewBag.cancelRedirectUrl = cancelRedirectUrl; string patientName = ""; Patient patient = model.PatientId > 0 ? unitOfWork.Repository <Patient>().Get(model.PatientId) : null; var appointment = unitOfWork.Repository <Appointment>() .Queryable() .Include(p => p.Patient) .SingleOrDefault(a => a.Patient.Id == patient.Id && a.AppointmentDate == model.AppointmentDate); if (appointment != null) { ModelState.AddModelError("AppointmentDate", "Patient already has an appointment for this date"); } if (model.AppointmentDate > DateTime.Today.AddYears(2)) { ModelState.AddModelError("AppointmentDate", "Appointment Date should be within 2 years"); } if (model.AppointmentDate < DateTime.Today) { ModelState.AddModelError("AppointmentDate", "Appointment Date should be after current date"); } if (ModelState.IsValid) { try { patientName = patient.FullName; var currentUser = unitOfWork.Repository <User>().Queryable().SingleOrDefault(u => u.UserName == User.Identity.Name); var newAppointment = new Appointment(patient) { AppointmentDate = model.AppointmentDate, Reason = model.Reason, DNA = false, Cancelled = false }; unitOfWork.Repository <Appointment>().Save(newAppointment); HttpCookie cookie = new HttpCookie("PopUpMessage"); cookie.Value = "Appointment record added successfully"; Response.Cookies.Add(cookie); return(Redirect("/Patient/PatientView.aspx?pid=" + newAppointment.Patient.Id.ToString())); } catch (Exception ex) { ModelState.AddModelError("AppointmentDate", string.Format("Unable to add the appointment: {0}", ex.Message)); } } ViewBag.PatientName = patientName; return(View(model)); }