public ActionResult Create(CreateEmployeeModel cModel) { using (var db = new HR_QueryEntities()) { cModel.DepartmentsDropDown.Add(new SelectListItem { Text = "Select an option...", Value = "0" }); cModel.LocationsDropDown.Add(new SelectListItem { Text = "Select an option...", Value = "0" }); cModel.PositionTypeDropDown.Add(new SelectListItem { Text = "Select an option...", Value = "0" }); var list = db.Departments.ToList(); var items = from g in list select new SelectListItem { Value = g.Dept_ID.ToString(), Text = g.Dept_Name }; foreach (var item in items) { cModel.DepartmentsDropDown.Add(item); } var list2 = db.Locations.ToList(); var items2 = from g in list2 select new SelectListItem { Value = g.Location_ID.ToString(), Text = g.Location_Name }; foreach (var item in items2) { cModel.LocationsDropDown.Add(item); } var list3 = db.Position_Types.ToList(); var items3 = from g in list3 select new SelectListItem { Value = g.Position_Type_ID.ToString(), Text = g.Position_Type_Name }; foreach (var item in items3) { cModel.PositionTypeDropDown.Add(item); } if (ModelState.IsValid) { Employee newEmployee = new Employee(); newEmployee.First_Name = cModel.EmployeeFirstName; newEmployee.Last_Name = cModel.EmployeeLastName; newEmployee.Department = int.Parse(cModel.DepartmentsDropDownSelection); newEmployee.Location = int.Parse(cModel.LocationsDropDownSelection); newEmployee.Position_Type = int.Parse(cModel.PositionTypeDropDownSelection); Request newRequest = new Request(); newRequest.Requestor_Name = User.Identity.Name; newRequest.Request_Status = true; newRequest.Employee_ID = (db.Employees.Select(x => x).ToList()).Last().Employee_ID + 1; newRequest.Request_Type_Index = 1; db.Employees.Add(newEmployee); db.Requests.Add(newRequest); db.SaveChanges(); return RedirectToAction("Options", "Home"); } ModelState.AddModelError("", "Please correct errors and resubmit."); return View(cModel); } }
public ActionResult Delete(EmployeeProfileModel pModel) { using (var db = new HR_QueryEntities()) { List<Request> query = db.Requests.Select(x => x).Where(x => x.Request_Status == true && x.Request_Type_Index == 2).ToList(); foreach (Request r in query) { if (r.Employee_ID == pModel.EmployeeID) return RedirectToAction("Options", "Home"); } Request newRequest = new Request(); newRequest.Requestor_Name = User.Identity.Name; newRequest.Request_Status = true; newRequest.Employee_ID = pModel.EmployeeID; newRequest.Request_Type_Index = 2; db.Requests.Add(newRequest); db.SaveChanges(); return RedirectToAction("Options", "Home"); } }