public async Task <IActionResult> Create(Course model) { if (!this.ModelState.IsValid) { var departments = _context.Departments.ToList(); this.ViewData["Departments"] = departments; return(this.View(model)); } var emptyCourse = new Course() { Title = model.Title, DepartmentID = model.DepartmentID, Credits = model.Credits, CourseID = model.CourseID }; if (await TryUpdateModelAsync <Course>( emptyCourse, "course", // Prefix for form value. s => s.CourseID, s => s.DepartmentID, s => s.Title, s => s.Credits)) { _context.Courses.Add(emptyCourse); await _context.SaveChangesAsync(); } return(RedirectToAction("Index", "Courses")); }
public async Task <IActionResult> Create(Department model) { if (!this.ModelState.IsValid) { var instructors = _context.Instructors.ToList(); this.ViewData["Instructors"] = instructors; return(this.View(model)); } var emptyDepartment = new Department() { Name = model.Name, Budget = model.Budget, StartDate = model.StartDate, InstructorID = model.InstructorID, }; if (await TryUpdateModelAsync <Department>( emptyDepartment, "department", // Prefix for form value. s => s.InstructorID, s => s.Name, s => s.Budget, s => s.StartDate)) { _context.Departments.Add(emptyDepartment); await _context.SaveChangesAsync(); } return(RedirectToAction("Index", "Departments")); }
public async Task <IActionResult> Edit(Student model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var studentToUpdate = await _context.Students.FindAsync(model.ID); if (await TryUpdateModelAsync <Student>( studentToUpdate, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { studentToUpdate.FirstMidName = model.FirstMidName; studentToUpdate.LastName = model.LastName; studentToUpdate.EnrollmentDate = model.EnrollmentDate; _context.Students.Update(studentToUpdate); await _context.SaveChangesAsync(); } return(RedirectToAction("Index", "Students")); }