public ActionResult Edit(int?id, Project thisProj) { if (Request.Cookies["UserID"].Value == null) { //Redirect to login if it can't find user id TempData["message"] = "Please log in."; System.Diagnostics.Debug.WriteLine("User not logged in. Redirecting to login page.\n"); return(RedirectToAction("LandingPage", "Home")); } ViewBag.UserID = Request.Cookies["UserID"].Value; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } bool Status = false; string message = ""; //Don't check include in validation check //ModelState.Remove("UserEmail"); //ModelState.Remove("EmpFName"); //ModelState.Remove("EmpLName"); //ModelState.Remove("EmpPhone"); //ModelState.Remove("EmpType"); if (ModelState.IsValid) { using (Entities dc = new Entities()) { GrizzTime.Models.project proj = dc.projects.FirstOrDefault(p => p.ProjID == id); if (thisProj == null) { return(HttpNotFound()); } proj.ProjName = thisProj.ProjName; proj.ProjDesc = thisProj.ProjDesc; proj.ProjStartDate = thisProj.ProjStartDate; proj.ProjManID = Int32.Parse(thisProj.ProjManID); proj.ConID = Int32.Parse(thisProj.ConID); if (thisProj.IsEnded) { proj.ProjStatus = "Ended"; if (thisProj.ProjEndDate == null) { proj.ProjEndDate = DateTime.Now; } } else { proj.ProjStatus = "Ongoing"; } dc.Entry(proj).State = System.Data.Entity.EntityState.Modified; try { dc.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception exception = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message1 = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); //create a new exception inserting the current one //as the InnerException exception = new InvalidOperationException(message1, exception); } } throw exception; } } message = "Project updated successfully."; Status = true; TempData["message"] = message; ViewBag.Status = Status; return(RedirectToAction("MyProjects", "home")); } else { message = "Invalid Request"; } TempData["message"] = message; ViewBag.Status = Status; return(View(thisProj)); }
public ActionResult Create(Project thisProj) { bool Status = false; string message = ""; if (Request.Cookies["UserID"].Value == null) { //Redirect to login if it can't find user id TempData["message"] = "Please log in."; System.Diagnostics.Debug.WriteLine("User not logged in. Redirecting to login page.\n"); return(RedirectToAction("LandingPage", "Home")); } //Can be a business user or a employee user ViewBag.UserID = Request.Cookies["UserID"].Value; //ensure that the model exists if (ModelState.IsValid) { using (Entities dc = new Entities()) { GrizzTime.Models.project proj = new GrizzTime.Models.project(); proj.ProjName = thisProj.ProjName; proj.ProjDesc = thisProj.ProjDesc; proj.ProjStartDate = thisProj.ProjStartDate; proj.ProjManID = Int32.Parse(thisProj.ProjManID); proj.ConID = Int32.Parse(thisProj.ConID); proj.ProjStatus = "Ongoing"; dc.projects.Add(proj); try { dc.SaveChanges(); message = "Project was successfully created."; } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception exception = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message1 = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); //create a new exception inserting the current one //as the InnerException exception = new InvalidOperationException(message1, exception); } } throw exception; } } } else { message = "Invalid Request"; } TempData["message"] = message; ViewBag.Status = Status; return(RedirectToAction("Create")); }