public ActionResult Create(EmployeeCreateNewViewModel viewModel) { Employee employee = viewModel.Employee; try { // TODO: Add insert logic here using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"INSERT INTO Employee (FirstName,LastName, IsSuperVisor, DepartmentId) VALUES (@FirstName, @LastName, @IsSuperVisor, @DepartmentId)"; cmd.Parameters.Add(new SqlParameter("@FirstName", employee.FirstName)); cmd.Parameters.Add(new SqlParameter("@LastName", employee.LastName)); cmd.Parameters.Add(new SqlParameter("@IsSuperVisor", employee.IsSuperVisor)); cmd.Parameters.Add(new SqlParameter("@DepartmentId", employee.DepartmentId)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
///////////////////////////created by alex -- for the GET i pass in a view model that contain a selectlistitem, i do this so i can create the drop down menu based on departments. i then do the post to insert into sql // GET: Employees/Create public ActionResult Create() { EmployeeCreateNewViewModel viewModel = new EmployeeCreateNewViewModel(); viewModel.AvailableDepartments = GetDepartments(); return(View(viewModel)); }