public ActionResult Edit(int id, InstructorCohortViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"UPDATE Instructor SET FirstName = @firstName, LastName = @lastName, SlackHandle = @slack, CohortId = @cohortId, Speciality = @speciality 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("@slack", viewModel.instructor.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@cohortId", viewModel.instructor.CohortId)); cmd.Parameters.Add(new SqlParameter("@speciality", viewModel.instructor.Specialty)); cmd.Parameters.Add(new SqlParameter("@id", id)); int rowsAffected = cmd.ExecuteNonQuery(); } } return(RedirectToAction(nameof(Index))); } catch { return(View(viewModel.instructor)); } }
public ActionResult Create(InstructorCohortViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"INSERT INTO Instructor ( FirstName, LastName, SlackHandle, CohortId, Speciality ) VALUES ( @firstName, @lastName, @slackHandle, @cohortId, @speciality )"; 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("@speciality", viewModel.instructor.Specialty)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
// GET: Instructors/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var instructor = await _context.Instructor.FindAsync(id); if (instructor == null) { return(NotFound()); } InstructorCohortViewModel vm = new InstructorCohortViewModel(); vm.instructor = await _context.Instructor .FirstOrDefaultAsync(m => m.Id == id); vm.cohorts = _context.Cohort.Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Name }).ToList(); vm.cohorts.Insert(0, new SelectListItem() { Value = "0", Text = "Please Choose a Cohort" }); return(View(vm)); }
// GET: Instructors/Create public async Task <IActionResult> Create() { var cohorts = await _context.Cohort.ToListAsync(); var viewModel = new InstructorCohortViewModel() { CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName") }; return(View(viewModel)); }
// GET: Instructors/Create public IActionResult Create() { //create an instance of your viewModel to view a list of cohorts with the instructors InstructorCohortViewModel ViewModel = new InstructorCohortViewModel(); //Then use the view model rather than view data for more flexibility ViewModel.cohorts = _context.Cohort.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() } ).ToList(); return(View(ViewModel)); }
// GET: Instructors/Create public IActionResult Create() { InstructorCohortViewModel vm = new InstructorCohortViewModel(); vm.cohorts = _context.Cohort.Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Name }).ToList(); vm.cohorts.Insert(0, new SelectListItem() { Value = "0", Text = "Please Choose a Cohort" }); return(View(vm)); }
public async Task <IActionResult> Create(InstructorCohortViewModel viewModel) { var instructor = viewModel.Instructor; if (ModelState.IsValid) { _context.Add(instructor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } viewModel = new InstructorCohortViewModel() { Instructor = instructor, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", instructor.CohortId) }; return(View(viewModel)); }
// GET: InstructorsController/Create public ActionResult Create() { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT Cohort.Id, Cohort.Name FROM Cohort"; SqlDataReader reader = cmd.ExecuteReader(); // Create a new instance of our view model InstructorCohortViewModel viewModel = new InstructorCohortViewModel(); 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(); // send it all to the view return(View(viewModel)); } } }
public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,SlackHandle,Specialty,CohortId")] Instructor instructor) { InstructorCohortViewModel ViewModel = new InstructorCohortViewModel(); if (id != instructor.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var user = await GetCurrentUserAsync(); instructor.UserId = user.Id; _context.Update(instructor); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InstructorExists(instructor.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewModel.cohorts = _context.Cohort.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() } ).ToList(); return(View(ViewModel)); }
public async Task <IActionResult> Edit(int id, InstructorCohortViewModel viewModel) { var instructor = viewModel.Instructor; if (id != instructor.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(instructor); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InstructorExists(instructor.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } var cohorts = await _context.Cohort.ToListAsync(); viewModel = new InstructorCohortViewModel() { Instructor = instructor, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", instructor.CohortId) }; return(View(viewModel)); }
// GET: Instructors/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var instructor = await _context.Instructor.FindAsync(id); if (instructor == null) { return(NotFound()); } var cohorts = await _context.Cohort.ToListAsync(); var viewModel = new InstructorCohortViewModel() { Instructor = instructor, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", instructor.CohortId) }; return(View(viewModel)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,SlackHandle,Specialty,CohortId")] Instructor instructor) { InstructorCohortViewModel ViewModel = new InstructorCohortViewModel(); if (ModelState.IsValid) { // get the current user var user = await GetCurrentUserAsync(); instructor.UserId = user.Id; _context.Add(instructor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewModel.cohorts = _context.Cohort.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() } ).ToList(); return(View(ViewModel)); }
// GET: Instructors/Edit/5 public async Task <IActionResult> Edit(int?id) { InstructorCohortViewModel ViewModel = new InstructorCohortViewModel(); if (id == null) { return(NotFound()); } ViewModel.instructor = await _context.Instructor.FindAsync(id); if (ViewModel.instructor == null) { return(NotFound()); } ViewModel.cohorts = _context.Cohort.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() } ).ToList(); return(View(ViewModel)); }
// GET: InstructorsController/Edit/5 public ActionResult Edit(int id) { InstructorCohortViewModel viewModel = new InstructorCohortViewModel(); 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.Speciality FROM Instructor i WHERE i.Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); viewModel.instructor = null; if (reader.Read()) { viewModel.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("Speciality")) }; } reader.Close(); } using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT Cohort.Id, Cohort.Name FROM Cohort"; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Cohort cohort = new Cohort { Id = reader.GetInt32(reader.GetOrdinal("Id")), Name = reader.GetString(reader.GetOrdinal("Name")) }; SelectListItem cohortOptionTag = new SelectListItem() { Text = cohort.Name, Value = cohort.Id.ToString() }; viewModel.cohorts.Add(cohortOptionTag); } reader.Close(); } if (viewModel.instructor != null) { return(View(viewModel)); } else { return(RedirectToAction(nameof(NotFound))); } } }