public ActionResult Create(MissionsModel m) { if (Session["ID"] == null) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { int id = Convert.ToInt32(Session["ID"]); if (m.PeopleInCharge != null) { Mission mission = new Mission(); try { mission.id = db.Missions.OrderByDescending(x => x.id).FirstOrDefault().id + 1; } catch (Exception) { mission.id = 1; } mission.PersonEntered = id; mission.Manager = m.ManagerId; mission.MissionText = m.mission.MissionText; mission.DateOfEntry = DateTime.Now; mission.TimeOfEntry = DateTime.Now.TimeOfDay; db.Missions.Add(mission); if (m.PeopleInCharge != null) { int picNum; try { picNum = db.MissionPersonInCharges.OrderByDescending(x => x.id).FirstOrDefault().id + 1; } catch (Exception) { picNum = 1; } for (int i = 0; i < m.PeopleInCharge.Length; i++) { MissionPersonInCharge personIncharge = new MissionPersonInCharge(); personIncharge.id = picNum; personIncharge.MissionId = mission.id; personIncharge.EmployeeID = m.PeopleInCharge[i]; picNum++; db.MissionPersonInCharges.Add(personIncharge); } } db.SaveChanges(); return(RedirectToAction("Index")); } else { ViewBag.PeopleInChargeError = "الرجاء اختيار المسؤلين عن المهمة"; } } var empid = Convert.ToInt32(Session["ID"]); var pp = db.MissionPersonInCharges.Where(x => x.MissionId == m.mission.id).Select(x => x.EmployeeID).ToList(); ViewBag.ManagerId = new SelectList(db.Employees.Where(x => x.State != "خارج الخدمة").OrderBy(x => x.id).ToList(), m.mission.Manager); ViewBag.PeopleInCharge = new MultiSelectList(ViewBag.PeopleInCharge = db.Employees.Where(x => x.State != "خارج الخدمة").OrderBy(x => x.id).ToList(), pp); ViewBag.TitleSideBar = "Missions"; return(View(m)); }
public ActionResult Edit(MissionsModel m) { if (Session["ID"] == null) { return(RedirectToAction("Index", "Home")); } int id = Convert.ToInt32(Session["ID"]); if (m.PeopleInCharge != null) { if (ModelState.IsValid) { var mission = db.Missions.Find(m.mission.id); mission.MissionText = m.mission.MissionText; //Missions.Manager = db.Employees.Find(m.managerID).username; mission.Manager = m.ManagerId; mission.DateOfLastModification = DateTime.Now; mission.TimeOfLastModification = DateTime.Now.TimeOfDay; mission.Checked = null; mission.Closed = null; if (mission.NumberOfModifications == null) { mission.NumberOfModifications = 1; } else { mission.NumberOfModifications++; } var b = db.MissionPersonInCharges.Where(c => c.MissionId == m.mission.id).ToList(); foreach (var item in b) { db.MissionPersonInCharges.Remove(item); } if (m.PeopleInCharge != null) { int picNum; try { picNum = db.MissionPersonInCharges.OrderByDescending(x => x.id).FirstOrDefault().id + 1; } catch (Exception) { picNum = 1; } for (int i = 0; i < m.PeopleInCharge.Length; i++) { var personIncharge = new MissionPersonInCharge(); personIncharge.id = picNum; personIncharge.MissionId = mission.id; personIncharge.EmployeeID = m.PeopleInCharge[i]; picNum++; db.MissionPersonInCharges.Add(personIncharge); } } db.Entry(mission).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } else { ViewBag.PeopleInChargeError = "الرجاء اختيار المسؤلين عن المهمة"; } var empid = Convert.ToInt32(Session["ID"]); var pp = db.MissionPersonInCharges.Where(x => x.MissionId == m.mission.id).Select(x => x.EmployeeID).ToList(); ViewBag.ManagerId = new SelectList(db.Employees.Where(x => x.State == "متوفر").Where(x => x.id != empid).OrderBy(x => x.id).ToList(), m.mission.Manager); ViewBag.PeopleInCharge = new MultiSelectList(ViewBag.PeopleInCharge = db.Employees.Where(x => x.State == "متوفر").Where(x => x.id != empid).OrderBy(x => x.id).ToList(), pp); ViewBag.TitleSideBar = "Missions"; return(View(m)); }