public ActionResult RemoveEmpFromProject(int pid, int eid) { string message = ""; using (Entities dc = new Entities()) { project proj = dc.projects.Find(pid); //Get employee_project row where employeeID and projectID match the parametes employee_project empproj = dc.employee_project.Where(m => m.EmpID == eid && m.ProjID == pid).SingleOrDefault(); dc.employee_project.Remove(empproj); 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 = "Employee removed successfully."; TempData["message"] = message; return(RedirectToAction("Details", new { id = pid })); }
public ActionResult AddEmpToProject(int id, Employee addEmp) { 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; string message = ""; using (Entities dc = new Entities()) { project proj = dc.projects.Find(id); ViewBag.ProjectName = proj.ProjName; ViewBag.ProjectID = proj.ProjID; ViewBag.ProjectManager = proj.ProjManID; if (proj == null) { message = "Project not found."; TempData["message"] = message; //TODO: Redirect to somewhere more general. return(RedirectToAction("MyProjects", "Home")); } employee_project empproj = new employee_project() { EmpID = addEmp.UserID, ProjID = id, StartDate = DateTime.Now, }; dc.employee_project.Add(empproj); 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 = "Employee added successfully."; TempData["message"] = message; return(View()); }