public ActionResult Edit(int id, TrainingProgramEditView viewModel) { TrainingProgram trainingProgram = viewModel.TrainingProgram; try { var updatedTrainingProgram = viewModel.TrainingProgram; using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE TrainingProgram SET Name = @name, StartDate = @startDate, EndDate = @endDate, MaxAttendees = @maxAttendees WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@name", updatedTrainingProgram.Name)); cmd.Parameters.Add(new SqlParameter("@startDate", updatedTrainingProgram.StartDate)); cmd.Parameters.Add(new SqlParameter("@endDate", updatedTrainingProgram.EndDate)); cmd.Parameters.Add(new SqlParameter("@maxAttendees", updatedTrainingProgram.MaxAttendees)); cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.ExecuteNonQuery(); } } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
// GET: TrainingProgram/Edit/5 public ActionResult Edit(int id) { // Get the training program TrainingProgram trainingProgram = GetTrainingProgram(id); DateTime currentDate = DateTime.Now; // Check if training program start date is in the future. If it is, instatiate the EditView so it can be edited, otherwise take the user back to the index view if (trainingProgram.StartDate > currentDate) { TrainingProgramEditView viewModel = new TrainingProgramEditView { TrainingProgram = trainingProgram }; return(View(viewModel)); } return(RedirectToAction(nameof(Index))); }