public ActionResult DeleteConfirmed(int id) { EmpPos empPos = db.EmpPos.Find(id); db.EmpPos.Remove(empPos); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "EmpPosID,EmployeeID,PositionID,Started,Ended")] EmpPos empPos) { if (ModelState.IsValid) { db.Entry(empPos).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "Name", empPos.EmployeeID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", empPos.PositionID); return(View(empPos)); }
// GET: EmpPos/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } EmpPos empPos = db.EmpPos.Find(id); if (empPos == null) { return(HttpNotFound()); } return(View(empPos)); }
// GET: EmpPos/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } EmpPos empPos = db.EmpPos.Find(id); if (empPos == null) { return(HttpNotFound()); } ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "Name", empPos.EmployeeID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", empPos.PositionID); return(View(empPos)); }
// GET: EmpPos public ActionResult Index() { List <EmpPos> empPosList = new List <EmpPos>(); var empPos = (from x in db.EmpPos select x).Include(e => e.Position).ToList(); foreach (var item in empPos) { EmpPos ep = new EmpPos(); ep.EmpPosID = item.EmpPosID; ep.EmployeeID = item.EmployeeID; ep.PositionID = item.PositionID; ep.Started = item.Started; if (ep.Ended == null) { ViewBag.EndDate = "Still Employed"; //ep.Ended = "Still Employed"; } } //db.EmpPos.Include(e => e.Employee).Include(e => e.Position); return(View(empPos.ToList())); }
public ActionResult AddEmployee([Bind(Include = "EmployeeID,Name,Surname,IDNumber,DateHired,ContactNum,KinContactNum,Email,FarmID,PositionID,GroupID,UserName,Password,ConfirmPassword,Type")] EmployeeAccountsViewModel employeeAccounts) { // employeeAccounts.Type = "Employee"; if (ModelState.IsValid) { try { // add employee account to account table var varBefore = db.Accounts.Select(x => x.AccountID).Count(); int before = Convert.ToInt16(varBefore); Account account = new Account(); account.UserName = employeeAccounts.UserName; account.Password = employeeAccounts.Password; account.ConfirmPassword = employeeAccounts.ConfirmPassword; db.Accounts.Add(account); db.SaveChanges(); //Acc ID var accFind = db.Accounts.Where(x => x.UserName == account.UserName).Select(y => y.AccountID).Single(); int accId = Convert.ToInt16(accFind); // add employee details to employee table var varAfter = db.Accounts.Select(x => x.AccountID).Count(); int after = Convert.ToInt16(varAfter); var beforeEmployeeIsAdded = db.Employees.Select(x => x.EmployeeID).Count(); if (before < after) { int positionId = employeeAccounts.PositionID; int farmId = employeeAccounts.FarmID; int groupId = employeeAccounts.GroupID; Employee employees = new Employee(); //employees.Account = new Account(); //employees.Position = new Position(); employees.EmployeeID = employeeAccounts.EmployeeID; employees.Email = employeeAccounts.Email; employees.Name = employeeAccounts.Name; employees.Surname = employeeAccounts.Surname; employees.AccountID = accId; employees.KinContactNum = employeeAccounts.KinContactNum; employees.IDNumber = employeeAccounts.IDNumber; employees.ContactNum = employeeAccounts.ContactNum; employees.DateHired = employeeAccounts.DateHired; DateTime hired = employeeAccounts.DateHired; employees.PositionID = positionId; employees.FarmID = farmId; employees.GroupID = groupId; // employees.Account.UserName = employeeAccounts.UserName; db.Employees.Add(employees); db.SaveChanges(); var checkEmp = db.Employees.Select(x => x.EmployeeID).Count(); if (beforeEmployeeIsAdded < checkEmp) { EmpPos employeePosition = new EmpPos(); employeePosition.EmployeeID = employees.EmployeeID; employeePosition.Started = hired; employeePosition.PositionID = positionId; employeePosition.Ended = null; db.EmpPos.Add(employeePosition); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception ex) { // undo account commit if employee transaction fails var acc = db.Accounts.Where(x => x.UserName == employeeAccounts.UserName).Select(y => y.AccountID).Single(); int ac = Convert.ToInt16(acc); AccountsController emp = new AccountsController(); emp.DeleteConfirmed(ac); ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); ViewBag.fatalError = ex.ToString(); return(View(employeeAccounts)); } } ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); return(View(employeeAccounts)); }