public ActionResult Edit(int[] selectedEmpId, DateTime dateEdit) { var duties = db.DutyLists.Where(x => x.DateDuty == dateEdit).ToList(); // все дежурства с такой датой foreach (DutyList s in duties) //очищаем все на эту дату { db.Entry(s).State = EntityState.Deleted; db.DutyLists.Remove(s); } for (int i = 0; i < selectedEmpId.Length; i++) // содаем новые { Employee newEmployee = db.Employees.Find(selectedEmpId[i]); DutyList newDutyList = new DutyList() { DateDuty = dateEdit, Employee = newEmployee }; db.Entry(newDutyList).State = EntityState.Added; db.DutyLists.Add(newDutyList); } db.SaveChanges(); return(RedirectToAction("Index", new { start = dateEdit })); }
public ActionResult CreateVacation(int selectedEmpId, DateTime Start, DateTime Finish) { Employee newEmployee = db.Employees.Find(selectedEmpId); var vacations = db.Vacations.Include(x => x.Employee).Where(x => x.Employee.EmployeeId == selectedEmpId && (x.Start >= Start && x.Start <= Finish || x.Start < Start && x.Finish >= Start)).ToList(); if (vacations.Any()) { foreach (Vacation vac in vacations) { db.Entry(vac).State = EntityState.Modified; vac.Start = Start; vac.Finish = Finish; } } else { Vacation newVacation = new Vacation(newEmployee, Start, Finish); db.Vacations.Add(newVacation); } db.SaveChanges(); return(RedirectToAction("Index", new { start = Start })); }