public ActionResult CreateEmployee(string company = null) { var model2 = context.States; var model = new CreateEmployeeVM { CompanyList = context.Companies .Where(c => company == null || c.CompanyName == company) .OrderBy(c => c.CompanyName) .Select(c => new SelectListItem { Text = c.CompanyName, Value = c.ID.ToString()}) }; return View(model); }
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); }