// GET: Instructors/Edit/5 public ActionResult Edit(int id) { Instructor instructor = GetInstructorById(id); if (instructor == null) { return(NotFound()); } InstructorEditViewModel viewModel = new InstructorEditViewModel { Cohorts = GetAllCohorts(), Instructor = instructor }; return(View(viewModel)); }
public IActionResult Get(int id) { Instructor instructor = _instructorsRepository .GetSingle(i => i.ID == id, i => i.CourseAssignments, i => i.OfficeAssignment); if (instructor != null) { InstructorEditViewModel instructorDetailsVM = Mapper.Map <Instructor, InstructorEditViewModel>(instructor); instructorDetailsVM.AssignedCourses = _instructorsRepository.GetAssignedCourses(instructor); return(new OkObjectResult(instructorDetailsVM)); } else { return(NotFound()); } }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { Instructor instructor = GetInstructorById(id); if (instructor == null) { return(NotFound()); } //This piece of code keeps the type correct to pass into the edit InstructorEditViewModel viewModel = new InstructorEditViewModel { Cohorts = GetAllCohorts(), Instructor = instructor }; return(View(viewModel)); }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { var viewModel = new InstructorEditViewModel(); var cohorts = GetAllCohorts(); var instructor = GetInstructorById(id); var selectItems = cohorts .Select(cohort => new SelectListItem { Text = cohort.Name, Value = cohort.Id.ToString() }) .ToList(); viewModel.Cohorts = selectItems; viewModel.Instructor = instructor; return(View(viewModel)); }
public ActionResult Edit([FromForm] InstructorEditViewModel model, int id) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" UPDATE Instructor SET FirstName = @firstName, LastName = @lastName, SlackHandle = @slackHandle, Speciality = @speciality, CohortId = @cId WHERE Instructor.Id = @id "; cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.Parameters.Add(new SqlParameter("@firstName", model.Instructor.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", model.Instructor.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", model.Instructor.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@speciality", model.Instructor.Speciality)); cmd.Parameters.Add(new SqlParameter("@cId", model.Instructor.CohortId)); int rowsAffected = cmd.ExecuteNonQuery(); /*if (rowsAffected > 0) * { * return new StatusCodeResult(StatusCodes.Status204NoContent); * } * * throw new Exception("No rows affected");*/ } } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { var viewModel = new InstructorEditViewModel(); viewModel.Cohorts = cohortList(); Instructor instructor = null; using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT Id, FirstName, LastName, SlackHandle, CohortId FROM Instructor WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { instructor = new Instructor() { Id = reader.GetInt32(reader.GetOrdinal("Id")), FirstName = reader.GetString(reader.GetOrdinal("FirstName")), LastName = reader.GetString(reader.GetOrdinal("LastName")), SlackHandle = reader.GetString(reader.GetOrdinal("SlackHandle")), CohortId = reader.GetInt32(reader.GetOrdinal("CohortId")) }; } } } foreach (var cohortItem in viewModel.Cohorts) { if (Convert.ToInt32(cohortItem.Value) == instructor.CohortId) { cohortItem.Selected = true; } } viewModel.Instructor = instructor; return(View(viewModel)); }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { var instructor = GetInstructorById(id); var cohortOptions = GetCohortOptions(); var viewModel = new InstructorEditViewModel() { InstructorId = instructor.Id, FirstName = instructor.FirstName, LastName = instructor.LastName, CohortId = instructor.CohortId, Specialty = instructor.Specialty, SlackHandle = instructor.SlackHandle, CohortOptions = cohortOptions // use a helper method GetCohortOptions to get cohorts below }; return(View(viewModel)); }
// GET: Instructor/Edit/5 public ActionResult Edit(int id) { Instructor instructor = null; InstructorEditViewModel model = new InstructorEditViewModel(Connection); using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT i.Id, i.FirstName, i.LastName, i.SlackHandle, i.CohortId, i.Specialty FROM Instructor i WHERE Id = @id "; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { instructor = new Instructor { Id = reader.GetInt32(reader.GetOrdinal("Id")), FirstName = reader.GetString(reader.GetOrdinal("FirstName")), LastName = reader.GetString(reader.GetOrdinal("LastName")), SlackHandle = reader.GetString(reader.GetOrdinal("SlackHandle")), CohortId = reader.GetInt32(reader.GetOrdinal("CohortId")), Specialty = reader.GetString(reader.GetOrdinal("Specialty")) }; } reader.Close(); } } model.Instructor = instructor; return(View(model)); }
public ActionResult Edit(int id, InstructorEditViewModel viewModel) { var updatedInstructor = viewModel.Instructor; try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Instructor SET FirstName = @firstName, LastName = @lastName, SlackHandle = @slackHandle, Speciality = @speciality, CohortId = @cohortId WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@firstName", updatedInstructor.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", updatedInstructor.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", updatedInstructor.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@speciality", updatedInstructor.Speciality)); cmd.Parameters.Add(new SqlParameter("@cohortId", updatedInstructor.CohortId)); cmd.Parameters.Add(new SqlParameter("@id", id)); cmd.ExecuteNonQuery(); } } return(RedirectToAction(nameof(Index))); } catch { viewModel = new InstructorEditViewModel() { Instructor = updatedInstructor, Cohorts = GetAllCohorts() }; return(View(viewModel)); } }
public async Task <ActionResult> Edit(int id, InstructorEditViewModel model) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT s.Id, s.firstName, s.lastName, s.slackHandle, s.cohortId FROM Instructor s WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); Instructor thisInstructor = null; if (reader.Read()) { thisInstructor = new Instructor { Id = reader.GetInt32(reader.GetOrdinal("Id")), firstName = reader.GetString(reader.GetOrdinal("FirstName")), lastName = reader.GetString(reader.GetOrdinal("LastName")), slackHandle = reader.GetString(reader.GetOrdinal("SlackHandle")), cohortId = reader.GetInt32(reader.GetOrdinal("CohortId")) }; } InstructorEditViewModel viewModel = new InstructorEditViewModel(_config.GetConnectionString("DefaultConnection")); viewModel.Instructor = thisInstructor; return(View(viewModel)); } } }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT TeacherId, FirstName, LastName, SlackHandle, CohortId FROM Instructor WHERE TeacherId = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); Instructor instructor = null; if (reader.Read()) { instructor = new Instructor { Id = reader.GetInt32(reader.GetOrdinal("TeacherId")), FirstName = reader.GetString(reader.GetOrdinal("FirstName")), LastName = reader.GetString(reader.GetOrdinal("LastName")), SlackHandle = reader.GetString(reader.GetOrdinal("SlackHandle")), CohortId = reader.GetInt32(reader.GetOrdinal("CohortId")) }; } reader.Close(); InstructorEditViewModel InstructorEditViewModel = new InstructorEditViewModel(_config.GetConnectionString("DefaultConnection")); InstructorEditViewModel.Instructor = instructor; return(View(InstructorEditViewModel)); } } }
// GET: Instructors/Edit/5 public async Task <ActionResult> Edit(int id) { string sql = $@" SELECT i.Id, i.FirstName, i.LastName, i.SlackHandle, i.Specialty, i.CohortId FROM Instructor i WHERE i.Id = {id} "; using (IDbConnection conn = Connection) { Instructor instructor = await conn.QueryFirstAsync <Instructor>(sql); InstructorEditViewModel model = new InstructorEditViewModel(_config); model.instructor = instructor; return(View(model)); } }
public ActionResult Edit(int id, InstructorEditViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Instructors SET FirstName = @firstName, LastName = @lastName, SlackHandle = @slackHandle, CohortId = @cohortId, Specialty = @specialty WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@firstName", viewModel.instructor.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", viewModel.instructor.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", viewModel.instructor.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@cohortId", viewModel.instructor.CohortId)); cmd.Parameters.Add(new SqlParameter("@specialty", viewModel.instructor.Specialty)); cmd.Parameters.Add(new SqlParameter("@id", id)); int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { return(RedirectToAction(nameof(Index))); } throw new Exception("No rows affected"); } } } catch (Exception) { return(View(viewModel)); } }
public ActionResult Edit(int id, IFormCollection collection, InstructorEditViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Instructor SET InstFirstName = @instFirstName, InstLastName = @instLastName, InstSlackHandle = @instSlackHandle, InstCohort = @instCohort, InstSpeciality = @instSpeciality WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@instFirstName", viewModel.instructor.InstFirstName)); cmd.Parameters.Add(new SqlParameter("@instLastName", viewModel.instructor.InstLastName)); cmd.Parameters.Add(new SqlParameter("@instSlackHandle", viewModel.instructor.InstSlackHandle)); cmd.Parameters.Add(new SqlParameter("@instCohort", viewModel.instructor.InstCohort)); cmd.Parameters.Add(new SqlParameter("@instSpeciality", viewModel.instructor.InstSpeciality)); cmd.Parameters.Add(new SqlParameter("@id", id)); int rowsaffected = cmd.ExecuteNonQuery(); if (rowsaffected > 0) { return(RedirectToAction(nameof(Index))); } throw new Exception("no rows affected"); } } } catch { return(View()); } }
public ActionResult Edit(int id, InstructorEditViewModel model) { try { // TODO: Add update logic here using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Instructor SET FirstName = @firstName, LastName = @lastName, SlackHandle = @slackHandle, Specialty = @specialty, CohortId = @cohortId WHERE Id = @id"; cmd.Parameters.AddWithValue("@firstName", model.Instructor.FirstName); cmd.Parameters.AddWithValue("@lastName", model.Instructor.LastName); cmd.Parameters.AddWithValue("@slackHandle", model.Instructor.SlackHandle); cmd.Parameters.AddWithValue("@specialty", model.Instructor.Specialty); cmd.Parameters.AddWithValue("@cohortId", model.Instructor.CohortId); cmd.Parameters.AddWithValue("@id", id); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
// GET: InstructorsController/Edit/5 public ActionResult Edit(int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @" SELECT Id, FirstName, LastName, SlackHandle, CohortId, Specialty FROM Instructors WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader ViewInstructorreader = cmd.ExecuteReader(); InstructorEditViewModel viewModel = new InstructorEditViewModel(); if (ViewInstructorreader.Read()) { viewModel.instructor = new Instructor { Id = ViewInstructorreader.GetInt32(ViewInstructorreader.GetOrdinal("Id")), FirstName = ViewInstructorreader.GetString(ViewInstructorreader.GetOrdinal("FirstName")), LastName = ViewInstructorreader.GetString(ViewInstructorreader.GetOrdinal("LastName")), SlackHandle = ViewInstructorreader.GetString(ViewInstructorreader.GetOrdinal("SlackHandle")), CohortId = ViewInstructorreader.GetInt32(ViewInstructorreader.GetOrdinal("CohortId")), Specialty = ViewInstructorreader.GetString(ViewInstructorreader.GetOrdinal("Specialty")) }; } ViewInstructorreader.Close(); // Select all the cohorts cmd.CommandText = @"SELECT Cohorts.Id, Cohorts.Name FROM Cohorts"; SqlDataReader reader = cmd.ExecuteReader(); // Create a new instance of our view model while (reader.Read()) { // Map the raw data to our cohort model Cohort cohort = new Cohort { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")) }; // Use the info to build our SelectListItem SelectListItem cohortOptionTag = new SelectListItem() { Text = cohort.Name, Value = cohort.Id.ToString() }; // Add the select list item to our list of dropdown options viewModel.cohorts.Add(cohortOptionTag); } reader.Close(); // If we got something back, send it to the view if (viewModel.instructor != null) { // send it all to the view return(View(viewModel)); } else { // If not, send it to our custom not found page return(RedirectToAction(nameof(NotFound))); } } } }
// GET: Instructors/Edit/5 public ActionResult Edit(int id) { InstructorEditViewModel model = new InstructorEditViewModel(Connection, id); return(View(model)); }
public async Task <ActionResult> Edit(int id) { string sql = $@" SELECT i.Id, i.FirstName, i.LastName, i.Slack, i.CohortId FROM Instructor i WHERE i.Id = @id "; Instructor instructor = null; /* * Run the query above and create an instance of Instructor * populated with the data it returns */ using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = await cmd.ExecuteReaderAsync(); if (reader.Read()) { instructor = new Instructor { Id = reader.GetInt32(reader.GetOrdinal("Id")), FirstName = reader.GetString(reader.GetOrdinal("FirstName")), LastName = reader.GetString(reader.GetOrdinal("LastName")), Slack = reader.GetString(reader.GetOrdinal("Slack")), CohortId = reader.GetInt32(reader.GetOrdinal("CohortId")) }; } else { reader.Close(); return(NotFound()); } /* * Create an instance of your InstructorEditViewModel */ InstructorEditViewModel instructorEditView = new InstructorEditViewModel(_config.GetConnectionString("DefaultConnection")); /* * Assign the instructor you created to the .Instructor * property of your view model */ instructorEditView.Instructor = instructor; reader.Close(); return(View(instructorEditView)); } } }
public ActionResult Edit(int id) { var model = new InstructorEditViewModel(id); return(View(model)); }
public ActionResult Delete(InstructorEditViewModel viewModel) { return(DeleteOr404(viewModel.ModelObject.Id, viewModel)); }
public ActionResult Edit(int id, InstructorEditViewModel viewModel) { return(SaveOr404(viewModel, true)); }
public ActionResult Create(InstructorEditViewModel viewModel) { return(SaveOr404(viewModel)); }