public ActionResult Edit(int id, TrainingProgramEditViewModel 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());
            }
        }
Example #2
0
        public ActionResult Edit(int id, TrainingProgramEditViewModel viewModel)
        {
            try
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"UPDATE trainingprogram
                                            SET [name] = @trainName, 
                                                startDate = @startDate, 
                                                endDate = @endDate, 
                                                maxattendees = @max 
                                            WHERE id = @id";
                        cmd.Parameters.Add(new SqlParameter("@trainName", viewModel.TrainingProgram.Name));
                        cmd.Parameters.Add(new SqlParameter("@startDate", viewModel.TrainingProgram.StartDate));
                        cmd.Parameters.Add(new SqlParameter("@endDate", viewModel.TrainingProgram.EndDate));
                        cmd.Parameters.Add(new SqlParameter("@max", viewModel.TrainingProgram.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)
        {
            TrainingProgram trainingProgram = GetTrainingProgramById(id);
            DateTime        currentDate     = DateTime.Now;

            if (trainingProgram.StartDate > currentDate)
            {
                TrainingProgramEditViewModel viewModel = new TrainingProgramEditViewModel
                {
                    TrainingProgram = trainingProgram
                };
                return(View(viewModel));
            }

            return(Ok("You are not allowed to edit past training programs. Please use the browser's back button to continue."));
        }
        // GET: TrainingPrograms/Edit/5
        public ActionResult Edit(int id)
        {
            TrainingProgram program = GetProgramById(id);

            if (program == null)
            {
                return(NotFound());
            }

            TrainingProgramEditViewModel viewModel = new TrainingProgramEditViewModel
            {
                TrainingProgram = program
            };

            return(View(viewModel));
        }