public ActionResult AddHolidays(List <AddHolidays> AHD) { if (ModelState.IsValid) { using (mmpEntities mP = new mmpEntities()) { mP.Configuration.ProxyCreationEnabled = false; foreach (var i in AHD) { #region Holiday Already Exists var isHolidayAssigned = IsHolidayAssigned(i.hd_name, i.hy_id, i.hd_from.Date); if (isHolidayAssigned) { ModelState.AddModelError("HolidayAssigned", "Holiday Already added for the year"); return(View(AHD)); } #endregion DateTime startDate = StartOfWeek(DateTime.Today, DayOfWeek.Monday); DateTime endDate = startDate.AddDays(6); #region Validate dates if (i.hd_from.Date > i.hd_to.Date) { ModelState.AddModelError("InvalidDate", "To Date should be equal to or greater than start date"); return(View(AHD)); } if (i.hd_from.Date < endDate) { ModelState.AddModelError("InvalidDate", "Cannot add dates for previous and current weeks"); return(View(AHD)); } #endregion holiday_details hd = new holiday_details() { hd_name = i.hd_name, hd_from = i.hd_from, hd_to = i.hd_to, //hy_id = null, // Since we are not using holiday_year table anymore generated_by = 1 // AUTOMATE USER ID }; mP.holiday_details.Add(hd); } mP.SaveChanges(); ModelState.Clear(); return(Json(new { success = true, message = "Saved Successfully" }, JsonRequestBehavior.AllowGet)); } } return(View(AHD)); }
public ActionResult Delete(int id) { using (mmpEntities mP = new mmpEntities()) { DateTime startDate = StartOfWeek(DateTime.Today, DayOfWeek.Monday); DateTime endDate = startDate.AddDays(6); holiday_details hd = mP.holiday_details.Where(x => x.hd_id == id).FirstOrDefault <holiday_details>(); if (hd.hd_from > endDate) { mP.holiday_details.Remove(hd); mP.SaveChanges(); return(Json(new { success = true, message = "Deleted Successfully" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, message = "Cannot delete holiday entries for current and previous weeks!" }, JsonRequestBehavior.AllowGet)); } } }