public ActionResult Create(CreateEmployeeViewModel viewModel) { if (ModelState.IsValid) { var department = _db.Departments.Single(d => d.Id == viewModel.DepartmentId); var employee = new Employee(); employee.Name = viewModel.Name; employee.HireDate = viewModel.HireDate; department.Employees.Add(employee); _db.Save(); return(RedirectToAction("detail", "department", new { id = viewModel.DepartmentId })); } return(View(viewModel)); }
public ActionResult Create(CreateEmployeeViewModel createEmployee) { if (ModelState.IsValid) { var department = _db.Departments.Single(x => x.ID == createEmployee.DepartmentID); var employee = new Employees(); employee.Name = createEmployee.Name; employee.HireDate = createEmployee.HireDate; department.Employees.Add(employee); _db.Save(); return(RedirectToAction("Details", "Department", new { id = createEmployee.DepartmentID })); } return(View(createEmployee)); }
public ActionResult Create(CreateEmployeeViewModel model) { if (ModelState.IsValid) { var department = _db.Departments.Single(d => d.Id == model.DepartmentID); var employee = new Employee() { Name = model.Name, HireDate = model.HireDate }; department.Employess.Add(employee); _db.Save(); return(RedirectToAction("detail", "department", new { id = model.DepartmentID })); } return(View(model)); }
public ActionResult Create(CreateEmployeeViewModel model) { if (ModelState.IsValid) { var department = _db.Departments.Single(d => d.Id == model.DepartmentId); Employee employee = new Employee() { HireDate = model.HireDate, Name = model.Name }; department.Employees.Add(employee); _db.Save(); return(RedirectToAction("detail", "department", new { depatmentId = model.DepartmentId })); } else { return(View(model)); } }
public ActionResult Create(CreateEmployeeViewModel viewModel) { if (!ModelState.IsValid) { return(View(viewModel)); } var department = _db.Departments.SingleOrDefault(d => d.Id == viewModel.DepartmentId); department?.Employees.Add( new Employee { Name = viewModel.Name, HireDate = viewModel.HireDate, }); _db.Save(); return(RedirectToAction("details", "Department", new{ id = viewModel.DepartmentId })); }
public ActionResult Create(CreateEmployeeViewModel viewModel) { if (ModelState.IsValid) // was viewModel correctly put together? { // Save a new employee to the database var department = _db.Departments.Single(d => d.Id == viewModel.DepartmentId); var employee = new Employee(); employee.Name = viewModel.Name; employee.HireDate = viewModel.HireDate; department.Employees.Add(employee); // Entity framework will detect all changes and create an INSERT statement for a new Employee _db.Save(); // Don't forget to save to the database!!! return(RedirectToAction("detail", "department", new { id = viewModel.DepartmentId })); } return(View(viewModel)); }
public ActionResult Create(CreateEmployeeVM employee) { if (ModelState.IsValid == false) { View(employee); } var department = _departmentDataSource.Departments.Where(d => d.Id == employee.DepartmentId).First(); Employee newEmployee = new Employee { Name = employee.Name, HireDate = employee.HireDate }; department.Employees.Add(newEmployee); _departmentDataSource.Save(); return(RedirectToAction("Detail", "Department", new { id = employee.DepartmentId })); }
public ActionResult Create(CreateEmployeeViewModel evEmployeeViewModel) { if (ModelState.IsValid) { var model = _ds.Departments.FirstOrDefault(dep => dep.Id == evEmployeeViewModel.DepartmentId); if (model != null) { model.Employees.Add(new Employee() { Name = evEmployeeViewModel.Name }); } _ds.Save(); return(RedirectToAction("Detail", "Department", new { ID = evEmployeeViewModel.DepartmentId })); } else { return(View(evEmployeeViewModel)); } }
public ActionResult Add(CreateEmployeeViewModel viewModel) { if (ModelState.IsValid) { var department = _db.Departments.Single(x => x.Id == viewModel.DepartmentId); var employee = new Employee { Name = viewModel.Name, HiredDate = viewModel.HiredDate }; department.Employees.Add(employee); _db.Save(); return(RedirectToAction("details", "department", new { id = viewModel.DepartmentId })); } else { return(View(viewModel)); } }