public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCourses) { if (!ModelState.IsValid) { return(Page()); } var instructorToUpdate = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .FirstOrDefaultAsync(s => s.ID == id); if (await TryUpdateModelAsync <Instructor>( instructorToUpdate, "Instructor", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { if (String.IsNullOrWhiteSpace( instructorToUpdate.OfficeAssignment?.Location)) { instructorToUpdate.OfficeAssignment = null; } UpdateInstructorCourses(_context, selectedCourses, instructorToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateInstructorCourses(_context, selectedCourses, instructorToUpdate); PopulateAssignedCourseData(_context, instructorToUpdate); return(Page()); }
public async Task<IActionResult> OnPostAsync(int? id) { if (id == null) { return NotFound(); } var student = await _context.Students.FindAsync(id); if (student == null) { return NotFound(); } try { _context.Students.Remove(student); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return RedirectToAction("./Delete", new { id, saveChangesError = true }); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Instructor instructor = await _context.Instructors .Include(i => i.CourseAssignments) .SingleAsync(i => i.ID == id); if (instructor == null) { return(RedirectToPage("./Index")); } //see osa siit lasen instructori kustutada, kui seda osa polnud, siis delet vajutades tuli exception var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } //Student = await _context.Students.FindAsync(id); var student = await _context.Students .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id); //if (Student != null) if (Student == null) { return(NotFound()); } try { _context.Students.Remove(Student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id = id, saveChangesError = true })); } //return RedirectToPage("./Index"); }
public async Task <IActionResult> OnPostAsync(int?id) { /* * if (id == null) * { * return NotFound(); * } */ //Instructor = await _context.Instructors.FindAsync(id); Instructor instructor = await _context.Instructors .Include(i => i.CourseAssignments) .SingleAsync(i => i.ID == id); var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); /* * if (Instructor != null) * { * _context.Instructors.Remove(Instructor); * await _context.SaveChangesAsync(); * } */ _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { //if (!ModelState.IsValid) //{ // return Page(); //} //_context.Courses.Add(Course); //await _context.SaveChangesAsync(); //return RedirectToPage("./Index"); var emptyCourse = new Course(); 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(RedirectToPage("./Index")); } // Select DepartmentID if TryUpdateModelAsync fails. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return(Page()); }
//public async Task<IActionResult> OnPostAsync(int? id) //{ // if (id == null) // { // return NotFound(); // } // Student = await _context.Students.FindAsync(id); // if (Student != null) // { // _context.Students.Remove(Student); // await _context.SaveChangesAsync(); // } // return RedirectToPage("./Index"); //} //ÐÞ¸Ä4 public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Students.AsNoTracking().FirstOrDefaultAsync(m => m.ID == id); if (student == null) { return(NotFound()); } try { _context.Students.Remove(student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException) { return(RedirectToAction("./Delete", new { id = id, saveChangesError = true })); } }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (courseToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Course>( courseToUpdate, "course", //Prefix for form value. c => c.Credits, c => c.DepartmentID, c => c.Title)) { await _context.SaveChangesAsync(); RedirectToPage("./Index"); } _context.Attach(Course).State = EntityState.Modified; // Select DepartmentId if TryUpdateModelAsync fails PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } /*老的方法 * _context.Students.Add(Student); * await _context.SaveChangesAsync(); * return RedirectToPage("./Index"); * * */ //TryUpdateModelAsync<Student> 尝试使用 PageModel 的 PageContext 属性中已发布的表单值更新 emptyStudent 对象。 //TryUpdateModelAsync 仅更新列出的属性 (s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)。 //第二个自变量 ("student", // Prefix) 是用于查找值的前缀。 该自变量不区分大小写。 //已发布的表单值通过模型绑定转换为 Student 模型中的类型。 //使用 TryUpdateModel 更新具有已发布值的字段是一种最佳的安全做法,因为这能阻止过多发布 var emptyStudent = new Student(); if (await TryUpdateModelAsync <Student>(emptyStudent, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { _context.Students.Add(emptyStudent); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(null); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (courseToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync( courseToUpdate, "course", c => c.Title, c => c.Credits, c => c.DepartmentID)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(string[] selectedCourses) { var newInstructor = new Instructor(); if (selectedCourses != null) { newInstructor.CourseAssignments = new List <CourseAssignment>(); foreach (var course in selectedCourses) { var courseAddTo = new CourseAssignment { CourseID = int.Parse(course) }; newInstructor.CourseAssignments.Add(courseAddTo); } } if (await TryUpdateModelAsync <Instructor>( newInstructor, "Instructor", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { _context.Instructors.Add(newInstructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedCourseData(_context, newInstructor); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { //var emptyStudent = new Student(); //if (await TryUpdateModelAsync<Student> ( // emptyStudent, // "student", // s => s.FirstMidName, s=> s.LastName, s=> s.EnrollmentDate)){ // _context.Students.Add(emptyStudent); // await _context.SaveChangesAsync(); // return RedirectToPage("./Index"); //} //return Page(); //SetValues 方法通过从另一个 PropertyValues 对象读取值来设置此对象的值。 SetValues 使用属性名称匹配。 //视图模型类型不需要与模型类型相关,它只需要具有匹配的属性。 //使用 StudentVM 时需要更新 Create.cshtml 才能使用 StudentVM 而非 Student。 if (!ModelState.IsValid) { return(Page()); } var entry = _context.Add(new Student()); entry.CurrentValues.SetValues(StudentVM); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); { } }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { var studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Student>( studentToUpdate, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(Student.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); } return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var studentToUpdate = await _context.Students.FindAsync(id); _context.Attach(Student).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(Student.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Instructor instructor = await _context.Instructors .Include(i => i.Courses) .SingleAsync(i => i.ID == id); if (instructor == null) { return(RedirectToPage("./Index")); } var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Students.FindAsync(id); if (student == null) { return(NotFound()); } try { _context.Students.Remove(student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException ex) { _logger.LogError(ex, ErrorMessage); return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Course).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(Course.CourseID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Models.Student>( studentToUpdate, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { await _context.SaveChangesAsync(); Console.WriteLine($"Updated {studentToUpdate}"); return(RedirectToPage("./Index")); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Students.FindAsync(id); if (student == null) { return(NotFound()); } try { //Sets Entity's state to Deleted: _context.Students.Remove(student); //Posts an SQL DELETE request: await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */)//If there was a network error: { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .FirstOrDefaultAsync(m => m.DepartmentID == id); // null means Department was deleted by another user. if (departmentToUpdate == null) { return(await HandleDeletedDepartment()); } // Update the RowVersion to the value when this entity was // fetched. If the entity has been updated after it was // fetched, RowVersion won't match the DB RowVersion and // a DbUpdateConcurrencyException is thrown. // A second postback will make them match, unless a new // concurrency issue happens. _context.Entry(departmentToUpdate) .Property("RowVersion").OriginalValue = Department.RowVersion; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "Department", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save. " + "The department was deleted by another user."); return(Page()); } var dbValues = (Department)databaseEntry.ToObject(); await setDbErrorMessage(dbValues, clientValues, _context); // Save the current RowVersion so next postback // matches unless an new concurrency issue happens. Department.RowVersion = (byte[])dbValues.RowVersion; // Must clear the model error for the next postback. ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { //if (!ModelState.IsValid) //{ // return Page(); //} //_context.Attach(Course).State = EntityState.Modified; //try //{ // await _context.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!CourseExists(Course.CourseID)) // { // return NotFound(); // } // else // { // throw; // } //} //return RedirectToPage("./Index"); if (id == null) { return(NotFound()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (courseToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Course>( courseToUpdate, "course", // Prefix for form value. c => c.Credits, c => c.DepartmentID, c => c.Title)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); //_context.Courses.Add(emptyCourse); //await _context.SaveChangesAsync(); //return RedirectToPage("./Index"); } // Select DepartmentID if TryUpdateModelAsync fails. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .FirstOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } _context.Entry(departmentToUpdate) .Property("RowVersion").OriginalValue = Department.RowVersion; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "Department", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save. " + "The department was deleted by another user."); return(Page()); } var dbValues = (Department)databaseEntry.ToObject(); await setDbErrorMessage(dbValues, clientValues, _context); Department.RowVersion = (byte[])dbValues.RowVersion; ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Students.Add(Student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var entry = _context.Add(new Student()); entry.CurrentValues.SetValues(StudentVM); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { var emptyStudent = new Student(); if (await TryUpdateModelAsync <Student>(emptyStudent, "student", x => x.FirstMidName, x => x.LastName, x => x.EnrollmentDate)) { _context.Students.Add(Student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }
public static async Task <bool> Initialize(SchoolContext context) { await context.Database.EnsureCreatedAsync(); if (await HasAtLeastOneStudentEntry(context)) { return(false); } await context.Students.AddRangeAsync(DemoStudents()); await context.SaveChangesAsync(); await context.Courses.AddRangeAsync(DemoCourses()); await context.SaveChangesAsync(); await context.Enrollments.AddRangeAsync(DemoEnrollments()); await context.SaveChangesAsync(); return(true); }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task <IActionResult> OnPostAsync(string[] selectedCourses) { var newInstructor = new Instructor(); if (selectedCourses.Length > 0) { newInstructor.Courses = new List <Course>(); // Load collection with one DB call. _context.Courses.Load(); } // Add selected Courses courses to the new instructor. foreach (var course in selectedCourses) { var foundCourse = await _context.Courses.FindAsync(int.Parse(course)); if (foundCourse != null) { newInstructor.Courses.Add(foundCourse); } else { _logger.LogWarning("Course {course} not found", course); } } try { if (await TryUpdateModelAsync <Instructor>( newInstructor, "Instructor", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { _context.Instructors.Add(newInstructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(RedirectToPage("./Index")); } catch (Exception ex) { _logger.LogError(ex.Message); } PopulateAssignedCourseData(_context, Instructor); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { var emptyCourse = new Course(); if (await TryUpdateModelAsync <Course>(emptyCourse, "course", x => x.CourseID, s => s.DepartmentID, x => x.Title, x => x.Credits)) { _context.Courses.Add(Course); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int?id) { Student studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Student>( studentToUpdate, "student", s => s.FirstName, s => s.LastName, studentToUpdate => studentToUpdate.EnrollmentDate )) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); /* * if (!ModelState.IsValid) * { * return Page(); * } * * _context.Attach(Student).State = EntityState.Modified; * * try * { * await _context.SaveChangesAsync(); * } * catch (DbUpdateConcurrencyException) * { * if (!StudentExists(Student.ID)) * { * return NotFound(); * } * else * { * throw; * } * } * * return RedirectToPage("./Index"); */ }
public async Task <IActionResult> OnPostAsync(int id) { var studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } studentToUpdate.FirstMidName = Data.FirstMidName; studentToUpdate.LastName = Data.LastName; studentToUpdate.EnrollmentDate = Data.EnrollmentDate; await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }