public async Task <IActionResult> Create([Bind("SubjectID,Credits,DepartmentID,Title")] Subject subject) { if (ModelState.IsValid) { _context.Add(subject); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateDepartmentsDropDownList(subject.DepartmentID); return(View(subject)); }
public async Task <IActionResult> Create([Bind("DepartmentID,Name,TeacherID")] Department department) { if (ModelState.IsValid) { _context.Add(department); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["TeacherID"] = new SelectList(_context.Teachers, "TeacherID", "FirstName", department.TeacherID); return(View(department)); }
public async Task <IActionResult> Create([Bind("EnrollmentDate,FirstName,LastName")] Student student) { try { if (ModelState.IsValid) { _context.Add(student); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(student)); }
public async Task <IActionResult> Create([Bind("FirstName,HireDate,LastName,RoomAssignment")] Teacher teacher, string[] selectedSubjects) { if (selectedSubjects != null) { teacher.SubjectAssignments = new List <SubjectAssignment>(); foreach (var subject in selectedSubjects) { var subjectToAdd = new SubjectAssignment { TeacherID = teacher.TeacherID, SubjectID = int.Parse(subject) }; teacher.SubjectAssignments.Add(subjectToAdd); } } if (ModelState.IsValid) { _context.Add(teacher); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateRelatedSubject(teacher); return(View(teacher)); }