public async Task <IActionResult> Edit(int id, [Bind("Id,StudentId,FirstName,LastName,EnrollmentDate,CurrentSemestar,AcquiredCredits,EducationLevel")] Student 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))); } return(View(student)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,Degree,AcademicRank,OfficeNumber,HireDate")] Teacher teacher) { if (id != teacher.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(teacher); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TeacherExists(teacher.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(teacher)); }
public async Task <IActionResult> Edit(int id, CourseStudentsEditViewModel viewmodel) /*Edit(int id, [Bind("Id,Title,Credits,Semester,Programme,EducationLevel,FirstTeacherId,SecondTeacherId")] Course course)*/ { if (id != viewmodel.Course.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(viewmodel.Course); await _context.SaveChangesAsync(); IEnumerable <int> listStudents = viewmodel.SelectedStudents; IQueryable <Enrollment> toBeRemoved = _context.Enrollment.Where(s => !listStudents.Contains(s.StudentId) && s.CourseId == id); _context.Enrollment.RemoveRange(toBeRemoved); IEnumerable <int> existStudents = _context.Enrollment.Where(s => listStudents.Contains(s.StudentId) && s.CourseId == id).Select(s => s.StudentId); IEnumerable <int> newStudents = listStudents.Where(s => !existStudents.Contains(s)); foreach (int studentId in newStudents) { _context.Enrollment.Add(new Enrollment { StudentId = studentId, CourseId = id }); } //_context.Update(course); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(viewmodel.Course.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["FirstTeacherId"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", viewmodel.Course.FirstTeacherId); ViewData["SecondTeacherId"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", viewmodel.Course.SecondTeacherId); return(View(viewmodel)); }