public ActionResult Create([Bind(Include = "ShiftId,ShiftName,StateId")] tbl_shift tbl_shift, List <int> Employee) { Random rand = new Random(); int ra = 0; if (ModelState.IsValid) { if (Session["OnlineUser"] != null) { if (Session["UserRole"].Equals("SUPERADMIN")) { ViewBag.OnlineUser = Session["UserName"].ToString(); ViewBag.OnlineUserRole = Session["UserRole"].ToString(); ra = rand.Next(1111, 9999); while (db.tbl_shift.FirstOrDefault(x => x.StateId == ra) != null) { ra = rand.Next(1111, 9999); } tbl_shift.ShiftId = ra; db.tbl_shift.Add(tbl_shift); db.SaveChanges(); foreach (int item in Employee) { tbl_ShiftEmployee oEmployee = new tbl_ShiftEmployee(); int re = rand.Next(111111, 999999); while (db.tbl_ShiftEmployee.FirstOrDefault(f => f.ShiftEmployeeId == re) != null) { re = rand.Next(111111, 999999); } oEmployee.ShiftEmployeeId = re; oEmployee.EmployeeId = item; oEmployee.Shiftid = ra; db.tbl_ShiftEmployee.Add(oEmployee); db.SaveChanges(); } return(RedirectToAction("Index")); } else { return(RedirectToAction("Accessdenied", "Home")); } } else { return(RedirectToAction("Login", "Account")); } } ViewBag.StateId = new SelectList(db.tbl_State, "StateId", "StateName", tbl_shift.StateId); return(View(tbl_shift)); }
public ActionResult DeleteConfirmed(int id) { var em = db.tbl_ShiftEmployee.Where(x => x.Shiftid == id).ToList(); foreach (var item in em) { tbl_ShiftEmployee tbl_ShiftEmployee = db.tbl_ShiftEmployee.Find(item.ShiftEmployeeId); db.tbl_ShiftEmployee.Remove(tbl_ShiftEmployee); db.SaveChanges(); } tbl_shift tbl_shift = db.tbl_shift.Find(id); db.tbl_shift.Remove(tbl_shift); db.SaveChanges(); return(RedirectToAction("Index")); }