public ActionResult CreateEmployee(CreateEmployeeVM vm) { if (ModelState.IsValid) { var model = new Employee { FirstName = vm.FirstName, LastName = vm.LastName, PhoneNumber = vm.PhoneNumber, Email = vm.Email, JobTitle = vm.JobTitle, CompanyID = vm.CompanyID }; context.SaveEmployee(model); //DRJ - now let's build the card //DRJ - finding the newly created employee's id int employeeID = context.Employees .Where(e => e.LastName == model.LastName && e.CompanyID == model.CompanyID) .Select(e => e.ID) .FirstOrDefault(); var card = new Card { CardName = vm.FirstName + " " + vm.LastName + "'s Business Card", EmployeeID = employeeID }; //DRJ - creating the card context.SaveCard(card); int cardID = context.Cards .Where(c => c.CardName == card.CardName && c.EmployeeID == card.EmployeeID) .Select(c => c.ID) .FirstOrDefault(); //DRJ - sending the user to view their business card return RedirectToAction("Index", "Card", new { id = cardID }); } //need to repopulate list if there are model binding issues vm.CompanyList = context.Companies .OrderBy(c => c.CompanyName) .Select(c => new SelectListItem { Text = c.CompanyName, Value = c.ID.ToString() }); return View(vm); }
public void SaveEmployee(Employee employee) { if (employee.ID == 0) { context.Employees.Add(employee); context.SaveChanges(); } else { Employee dbEntry = context.Employees.Find(employee.ID); if (dbEntry != null) { dbEntry.FirstName = employee.FirstName; dbEntry.LastName = employee.LastName; dbEntry.PhoneNumber = employee.PhoneNumber; dbEntry.JobTitle = employee.JobTitle; dbEntry.CompanyID = employee.CompanyID; dbEntry.Email = employee.Email; context.SaveChanges(); } } }
public ActionResult Edit(CardAdminVM vm) { if (ModelState.IsValid) { var Card = new Card { CardName = vm.FirstName + " " + vm.LastName + "'s Business Card", EmployeeID = vm.EmployeeID }; var Employee = new Employee { ID = vm.EmployeeID, FirstName = vm.FirstName, LastName = vm.LastName, JobTitle = vm.JobTitle, PhoneNumber = vm.PhoneNumber, Email = vm.Email, CompanyID = vm.CompanyID }; context.SaveEmployee(Employee); //context.SaveCard(Card); return RedirectToAction("Index"); } //DRJ - getting the company list vm.CompanyList = context.Companies .OrderBy(c => c.CompanyName) .Select(c => new SelectListItem { Text = c.CompanyName, Value = c.ID.ToString() }); //DRJ - getting the state list vm.StateList = context.States.OrderBy(s => s.Abbreviation) .Select(s => new SelectListItem { Text = s.Name, Value = s.Abbreviation }); return View(vm); }