public async Task <IActionResult> Create([Bind("CourseID,Credits,DepartmentID,Title")] Course course) { if (ModelState.IsValid) { _context.Add(course); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateDepartmentsDropDownList(course.DepartmentID); return(View(course)); }
public async Task <IActionResult> Create([Bind("DepartmentID,Name,Budget,StartDate,InstructorID")] Department department) { if (ModelState.IsValid) { _context.Add(department); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["InstructorID"] = new SelectList(_context.Instructors, "ID", "FullName", department.InstructorID); return(View(department)); }
public async Task <IActionResult> Create([Bind("EnrollmentDate,FirstMidName,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("FirstMidName,HireDate,LastName,OfficeAssignment")] Instructor instructor, string[] selectedCourses) { if (selectedCourses != null) { instructor.CourseAssignments = new List <CourseAssignment>(); foreach (var course in selectedCourses) { var courseToAdd = new CourseAssignment { InstructorID = instructor.ID, CourseID = int.Parse(course) }; instructor.CourseAssignments.Add(courseToAdd); } } if (ModelState.IsValid) { _context.Add(instructor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedCourseData(instructor); return(View(instructor)); }