public IActionResult Details(int id) { vmRoasterGroupCreateOrEdit vmRoasterGroupCreateOrEdit = new vmRoasterGroupCreateOrEdit(); if (id != 0) { var roasterGroup = db.RoasterGroup.Include(c => c.RoasterGroupDetailsList).ThenInclude(c => c.Shift).FirstOrDefault(c => c.Id == id); vmRoasterGroupCreateOrEdit = new vmRoasterGroupCreateOrEdit() { Id = roasterGroup.Id, Name = roasterGroup.Name, }; vmRoasterGroupCreateOrEdit.RoasterGroupDetailsList = roasterGroup.RoasterGroupDetailsList; return(PartialView("Details", vmRoasterGroupCreateOrEdit)); } else { ViewBag.GuidelineMessage = "Access Denied!"; ViewBag.ErrorMessage = "You are not authorize to update roasterGroup."; return(PartialView("Error")); } }
// GET: HR/Employees/Create public IActionResult CreateOrEdit(int id) { vmRoasterGroupCreateOrEdit vmRoasterGroupCreateOrEdit = new vmRoasterGroupCreateOrEdit(); if (id != 0) { var roasterGroup = db.RoasterGroup.Include(c => c.RoasterGroupDetailsList).ThenInclude(x => x.Shift).FirstOrDefault(c => c.Id == id); vmRoasterGroupCreateOrEdit = new vmRoasterGroupCreateOrEdit() { Id = roasterGroup.Id, Name = roasterGroup.Name }; vmRoasterGroupCreateOrEdit.ShiftList = db.Shift.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList(); vmRoasterGroupCreateOrEdit.ShiftGroupDetailsList = roasterGroup.RoasterGroupDetailsList.Select(x => x.ShiftId).ToList(); } vmRoasterGroupCreateOrEdit.ShiftList = db.Shift.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList(); return(PartialView("CreateOrEdit", vmRoasterGroupCreateOrEdit)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> CreateOrEdit(vmRoasterGroupCreateOrEdit vmRoasterGroupCreateOrEdit) { if (ModelState.IsValid) { try { if (vmRoasterGroupCreateOrEdit.Id != 0) { var roasterGroup = db.RoasterGroup.Find(vmRoasterGroupCreateOrEdit.Id); var roasterGroupDetails = db.RoasterGroupDetails.Where(x => x.RoasterGroupId == roasterGroup.Id); db.RemoveRange(roasterGroupDetails); roasterGroup.Name = vmRoasterGroupCreateOrEdit.Name; roasterGroup.RoasterGroupDetailsList = vmRoasterGroupCreateOrEdit.ShiftGroupDetailsList.Select(x => new RoasterGroupDetails() { ShiftId = x }).ToList(); db.RoasterGroup.Attach(roasterGroup); db.Entry(roasterGroup).State = EntityState.Modified; bool isUpdated = await db.SaveChangesAsync() > 0; if (isUpdated) { ModelState.Clear(); vmRoasterGroupCreateOrEdit model = new vmRoasterGroupCreateOrEdit(); ViewBag.ValidationMessage = true; return(PartialView("CreateOrEdit", model)); } } else { RoasterGroup roasterGroup = new RoasterGroup { Name = vmRoasterGroupCreateOrEdit.Name, }; roasterGroup.RoasterGroupDetailsList = vmRoasterGroupCreateOrEdit.ShiftGroupDetailsList.Select(x => new RoasterGroupDetails() { ShiftId = x }).ToList(); db.Add(roasterGroup); bool isSaved = await db.SaveChangesAsync() > 0; if (isSaved) { ModelState.Clear(); vmRoasterGroupCreateOrEdit model = new vmRoasterGroupCreateOrEdit(); ViewBag.ValidationMessage = true; return(PartialView("CreateOrEdit", model)); } } } catch (Exception ex) { return(PartialView("Error")); } } ModelState.AddModelError("Validation Failed!", "Error"); if (vmRoasterGroupCreateOrEdit.Id != 0) { var roasterGroup = db.RoasterGroup.Include(c => c.RoasterGroupDetailsList).ThenInclude(x => x.Shift).FirstOrDefault(c => c.Id == vmRoasterGroupCreateOrEdit.Id); vmRoasterGroupCreateOrEdit.ShiftList = db.Shift.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList(); vmRoasterGroupCreateOrEdit.ShiftGroupDetailsList = roasterGroup.RoasterGroupDetailsList.Select(x => x.ShiftId).ToList(); } else { vmRoasterGroupCreateOrEdit.ShiftList = db.Shift.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList(); } ViewBag.ValidationMessage = false; return(PartialView("CreateOrEdit", vmRoasterGroupCreateOrEdit)); }