// GET: Cohorts/Edit/5 public ActionResult Edit(int id) { var viewModel = new CohortEditViewModel(); Cohort cohort = null; using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT Id, [Name] FROM Cohort WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { cohort = new Cohort() { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")), }; } } } viewModel.Cohort = cohort; return(View(viewModel)); }
public ActionResult Edit(int id, CohortEditViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Cohort SET CohortName = @cohortName WHERE id = @id;"; cmd.Parameters.Add(new SqlParameter("@cohortName", viewModel.CohortName)); cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View(viewModel)); } }
public ActionResult Edit(int id, CohortEditViewModel cohort) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Cohort SET Name = @name WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@name", cohort.Name)); // needs this @id to know which object in database to update cmd.Parameters.Add(new SqlParameter("@id", id)); var rowsAffected = cmd.ExecuteNonQuery(); } } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public ActionResult Create(CohortEditViewModel cohort) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"INSERT INTO Cohort (Name) OUTPUT INSERTED.Id VALUES (@Name)"; cmd.Parameters.Add(new SqlParameter("@Name", cohort.Name)); var id = (int)cmd.ExecuteScalar(); cohort.CohortId = id; // this sends you back to index after created return(RedirectToAction(nameof(Index))); } } } catch (Exception ex) { return(View()); } }
public ActionResult Edit(int id, CohortEditViewModel viewModel) { Cohort cohort = viewModel.Cohort; try { // TODO: Add update logic here using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = $@"UPDATE Cohort SET CohortName = @cohortname WHERE Id = @id;"; // parameters cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.Parameters.Add(new SqlParameter("@cohortname", cohort.CohortName)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
// GET: CohortsController/Create public ActionResult Create() { var cohortOptions = GetCohortOptions(); var viewModel = new CohortEditViewModel() { }; return(View()); }
// GET: Cohorts/Edit/5 public ActionResult Edit(int id) { Cohort cohort = GetCohortById(id); CohortEditViewModel viewModel = new CohortEditViewModel(); viewModel.Cohort = cohort; return(View(viewModel)); }
// GET: Cohorts/Edit/5 public ActionResult Edit(int id) { Cohort cohort = GetCohortById(id); CohortEditViewModel viewModel = new CohortEditViewModel { CohortName = cohort.Name }; return(View(viewModel)); }
//GET: Cohorts/Edit/5 public ActionResult Edit(int id) { var cohort = GetCohortById(id); var cohortOptions = GetCohortOptions(); var viewModel = new CohortEditViewModel() { Name = cohort.Name //CohortOptions = cohortOptions }; return(View(viewModel)); }
// ********************************* // GET: Cohorts/Edit/5 // ********************************* public ActionResult Edit(int id) { Cohort cohort = GetCohortById(id); if (cohort == null) { return(NotFound()); } CohortEditViewModel viewModel = new CohortEditViewModel { CohortName = cohort.CohortName }; return(View(viewModel)); }
// GET: Cohorts/Edit/5 public ActionResult Edit(int id) { //Here we are verifying that the correct cohort is there by using the cohort Cohort cohort = GetCohortById(id); if (cohort == null) { return(NotFound()); } // This is setting the view model based on the cohort item we created above. CohortEditViewModel viewModel = new CohortEditViewModel { Cohort = cohort }; return(View(viewModel)); }