コード例 #1
0
        public ActionResult AssignTrainingProgram(int id, EmployeeAssignTrainingProgramViewModel viewModel)
        {
            using (SqlConnection conn = Connection)
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM EmployeeTraining WHERE EmployeeId = @id;";
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = @"INSERT INTO EmployeeTraining
                                            ( EmployeeId, TrainingProgramId )
                                            VALUES
                                            ( @employeeId, @trainingProgramId )
                                        ";
                    foreach (var trainingProgramId in viewModel.SelectedTrainingProgramIds)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add(new SqlParameter("@employeeId", id));
                        cmd.Parameters.Add(new SqlParameter("@trainingProgramId", trainingProgramId));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            return(RedirectToAction(nameof(Details), new { id }));
        }
コード例 #2
0
        // GET: Employee/AssignTrainingProgram/5
        public ActionResult AssignTrainingProgram(int id)
        {
            var employee  = GetEmployeeById(id);
            var viewModel = new EmployeeAssignTrainingProgramViewModel()
            {
                Employee                   = employee,
                AllTrainingPrograms        = GetAllFutureTrainingPrograms(id),
                SelectedTrainingProgramIds = employee.TrainingPrograms.Select(t => t.Id).ToList()
            };

            return(View(viewModel));
        }