public ActionResult DeleteConfirmed(int id) { Appointment appointment = db.Appointments.Find(id); db.Appointments.Remove(appointment); db.SaveChanges(User.Identity.Name); return(RedirectToAction("Index")); }
public ActionResult Create(Appointment appointment, int?participant_Id) { if (ModelState.IsValid) { db.Appointments.Add(appointment); db.SaveChanges(User.Identity.Name); return(Redirect(Url.Content("~/Appointment/Edit/" + appointment.Id))); } return(View(appointment)); }
// GET: /Appointment/Create public ActionResult Create(int?participant_Id) { var currentUser = db.Users.First(x => x.NT == User.Identity.Name); var appointment = new Appointment { Participant_Id = participant_Id.Value, User_Id = currentUser.Id, Date = DateTime.Now }; return(View(appointment)); }
// GET: /Appointment/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Appointment appointment = db.Appointments.Find(id); if (appointment == null) { return(HttpNotFound()); } return(View(appointment)); }
//public ActionResult Edit(Appointment appointment, int[] CobbConditionsForCobb) public ActionResult Edit(Appointment appointment, int[] cobbConditionsForCobb) { if (ModelState.IsValid) { // db.Entry(appointment).State = EntityState.Modified; //await db.SaveChangesAsync(); var appointmentFromDb = db.Appointments.Include(a => a.Cobbs).Include(a => a.CobbConditions).First(x => x.Id == appointment.Id); appointmentFromDb.Date = appointment.Date; appointmentFromDb.Height = appointment.Height; appointmentFromDb.Weight = appointment.Weight; appointmentFromDb.Comment = appointment.Comment; appointmentFromDb.Participant_Id = appointment.Participant_Id; appointmentFromDb.User_Id = appointment.User_Id; if (appointment.Weight.HasValue && appointment.Height.HasValue) { if (appointment.Weight.Value > 0 && appointment.Height > 0) { decimal?BMIValue = (appointment.Weight / ((appointment.Height / 100) * (appointment.Height / 100))); appointmentFromDb.TheBMI = BMIValue; } } if (appointment.Cobbs != null) { if (appointment.Cobbs.Count > 0) { foreach (var cobb in appointment.Cobbs) { if (appointmentFromDb.Cobbs.Any(x => x.Id == cobb.Id) || cobb.Id != 0) { appointmentFromDb.Cobbs.First(x => x.Id == cobb.Id).CobbType_Id = cobb.CobbType_Id; appointmentFromDb.Cobbs.First(x => x.Id == cobb.Id).Angle = cobb.Angle; appointmentFromDb.Cobbs.First(x => x.Id == cobb.Id).IsRight = cobb.IsRight; } else { appointmentFromDb.Cobbs.Add(cobb); } } var cobbIds = appointment.Cobbs.Select(x => x.Id).ToList(); foreach ( var cobbFromDbDeleted in appointmentFromDb.Cobbs.Where(x => !cobbIds.Contains(x.Id)).ToList()) { appointmentFromDb.Cobbs.Remove(cobbFromDbDeleted); db.Entry(cobbFromDbDeleted).State = EntityState.Deleted; } } } if (appointment.Cobbs == null) { var appointmentCobbs = db.Appointments.Include(a => a.Cobbs).SingleOrDefault(a => a.Id == appointment.Id); if (appointmentCobbs != null) { foreach (var deleteCobb in appointmentCobbs.Cobbs.ToList()) { db.Cobbs.Remove(deleteCobb); } } } if (appointmentFromDb.CobbConditions != null) { foreach (var cobbCondition in appointmentFromDb.CobbConditions.ToList()) { appointmentFromDb.CobbConditions.Remove(cobbCondition); } } if (cobbConditionsForCobb != null) { var cobbConditionsToAdd = db.CobbConditions.Where(x => cobbConditionsForCobb.Contains(x.Id)).ToList(); foreach (var cobbConditionToAdd in cobbConditionsToAdd) { cobbConditionToAdd.Appointments.Add(appointmentFromDb); appointmentFromDb.CobbConditions.Add(cobbConditionToAdd); } } //await db.SaveChangesAsync(); db.SaveChanges(User.Identity.Name); return(Redirect(Url.Content("~/Participant/Edit/" + appointment.Participant_Id + "#tabs-2"))); //return RedirectToAction("Index"); } ViewBag.Participant_Id = new SelectList(db.Participants, "Id", "FirstName", appointment.Participant_Id); ViewBag.User_Id = new SelectList(db.Users, "Id", "FirstName", appointment.User_Id); ViewBag.SamplingTypes = db.SamplingTypes; return(View(appointment)); }