public async Task <IActionResult> Create(StudentCohortViewModel viewModel) { var student = viewModel.Student; if (ModelState.IsValid) { _context.Add(student); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } viewModel = new StudentCohortViewModel() { Student = student, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId) }; return(View(viewModel)); //if (ModelState.IsValid) //{ // _context.Add(student); // await _context.SaveChangesAsync(); // return RedirectToAction(nameof(Index)); //} //ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId); //return View(student); }
public ActionResult Edit(int id, StudentCohortViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { // When we submit the edit form, go ahead and update the db with the info we passed in cmd.CommandText = @"UPDATE Student SET firstName=@firstName, lastName=@lastName, slackHandle=@slackHandle, cohortId=@cohortId WHERE Id = @id"; cmd.Parameters.Add(new SqlParameter("@firstName", viewModel.student.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", viewModel.student.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", viewModel.student.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@cohortId", viewModel.student.CohortId)); cmd.Parameters.Add(new SqlParameter("@id", id)); int rowsAffected = cmd.ExecuteNonQuery(); } } return(RedirectToAction(nameof(Index))); } catch { return(View(viewModel.student)); } }
// GET: Students/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Student.FindAsync(id); if (student == null) { return(NotFound()); } //ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId); var cohorts = await _context.Cohort.ToListAsync(); var viewModel = new StudentCohortViewModel() { Student = student, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId) //CohortOptions = cohorts.Select(c => new SelectListItem //{ // Value = c.Id.ToString(), // Text = c.CohortName //}).ToList() }; return(View(viewModel)); }
public ActionResult Create(StudentCohortViewModel viewModel) { try { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"INSERT INTO Student ( FirstName, LastName, SlackHandle, CohortId ) VALUES ( @firstName, @lastName, @slackHandle, @cohortId )"; cmd.Parameters.Add(new SqlParameter("@firstName", viewModel.student.FirstName)); cmd.Parameters.Add(new SqlParameter("@lastName", viewModel.student.LastName)); cmd.Parameters.Add(new SqlParameter("@slackHandle", viewModel.student.SlackHandle)); cmd.Parameters.Add(new SqlParameter("@cohortId", viewModel.student.CohortId)); cmd.ExecuteNonQuery(); return(RedirectToAction(nameof(Index))); } } } catch { return(View()); } }
// GET: Students/Create public IActionResult Create() { //ViewData tells us we need a view model //ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", "Id"); //Instantiate a view model StudentCohortViewModel vm = new StudentCohortViewModel(); //Get all of the Cohorts from the database and select each cohort and turn it into a selectlistitem and then change the return into a list vm.cohorts = _context.Cohort.Select(c => new SelectListItem { //The value has to be a string because values in HTML are all strings Value = c.Id.ToString(), Text = c.Name }).ToList(); //This is a benefit of a view model. We were able to add a default option in the cohorts dropdown. //Create a select list item with a value of 0 and insert it at position 0. vm.cohorts.Insert(0, new SelectListItem() { Value = "0", Text = "Please Choose a Cohort" }); return(View(vm)); }
// GET: Students/Create public async Task <IActionResult> Create() { //ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", "CohortName"); //return View(); var cohorts = await _context.Cohort.ToListAsync(); var viewModel = new StudentCohortViewModel() { CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName") }; return(View(viewModel)); }
// GET: Students/Create public IActionResult Create() { //create an instance of your viewModel to view a list of cohorts with the instructors StudentCohortViewModel ViewModel = new StudentCohortViewModel(); //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)); }
public async Task <IActionResult> Edit(int id, StudentCohortViewModel viewModel) //public async Task<IActionResult> Edit(int id, //[Bind("Student.Id,Student.FirstName,Student.LastName,Student.Slack,Student.CohortId")] StudentCohortViewModel viewModel) { var student = viewModel.Student; if (id != student.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(student); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(student.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } //ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId); //return View(student); var cohorts = await _context.Cohort.ToListAsync(); viewModel = new StudentCohortViewModel() { Student = student, CohortOptions = new SelectList(_context.Cohort, "Id", "CohortName", student.CohortId) }; return(View(viewModel)); }
// GET: StudentsController/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 StudentCohortViewModel viewModel = new StudentCohortViewModel(); 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,CohortId")] Student student) { StudentCohortViewModel ViewModel = new StudentCohortViewModel(); if (id != student.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var user = await GetCurrentUserAsync(); student.UserId = user.Id; _context.Update(student); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(student.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 <ActionResult> Create(StudentCohortViewModel model) { string sql = $@" INSERT INTO Student (FirstName, LastName, SlackHandle, CohortId) VALUES ( '{model.student.FirstName}', '{model.student.LastName}', '{model.student.SlackHandle}', {model.student.CohortId} ); "; using (IDbConnection conn = Connection) { await conn.ExecuteAsync(sql); return(RedirectToAction(nameof(Index))); } }
// GET: Students/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Student.FindAsync(id); if (student == null) { return(NotFound()); } StudentCohortViewModel vm = new StudentCohortViewModel(); // Get a single student from the database where the students ID matches the ID in the browser vm.student = await _context.Student .FirstOrDefaultAsync(m => m.Id == id); //Get all of the Cohorts from the database and select each cohort and turn it into a selectlistitem and then change the return into a list vm.cohorts = _context.Cohort.Select(c => new SelectListItem { //The value has to be a string because values in HTML are all strings Value = c.Id.ToString(), Text = c.Name }).ToList(); //This is a benefit of a view model. We were able to add a default option in the cohorts dropdown. //Create a select list item with a value of 0 and insert it at position 0. vm.cohorts.Insert(0, new SelectListItem() { Value = "0", Text = "Please Choose a Cohort" }); return(View(vm)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,SlackHandle,CohortId")] Student student) { StudentCohortViewModel ViewModel = new StudentCohortViewModel(); if (ModelState.IsValid) { //get the current user var user = await GetCurrentUserAsync(); student.UserId = user.Id; _context.Add(student); 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: Students/Edit/5 public async Task <IActionResult> Edit(int?id) { StudentCohortViewModel ViewModel = new StudentCohortViewModel(); if (id == null) { return(NotFound()); } ViewModel.student = await _context.Student.FindAsync(id); if (ViewModel.student == null) { return(NotFound()); } ViewModel.cohorts = _context.Cohort.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() } ).ToList(); return(View(ViewModel)); }
// GET: StudentsController/Edit/5 public ActionResult Edit(int id) { StudentCohortViewModel viewModel = new StudentCohortViewModel(); 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 Student s WHERE s.Id = @id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); viewModel.student = null; if (reader.Read()) { viewModel.student = new Student { 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")) }; } 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.student != null) { return(View(viewModel)); } else { return(RedirectToAction(nameof(NotFound))); } } }
// GET: Students/Create // This is the 'Create' view public ActionResult Create() { var model = new StudentCohortViewModel(_config); return(View(model)); }