// 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(Category).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(Category.ID)) { 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, string[] selectedCategories) { if (id == null) { return(NotFound()); } var bookToUpdate = await _context.Book .Include(i => i.Publisher) .Include(i => i.BookCategories) .ThenInclude(i => i.Category) .FirstOrDefaultAsync(s => s.ID == id); if (bookToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Book>( bookToUpdate, "Book", i => i.Title, i => i.Author, i => i.Price, i => i.PublishingDate, i => i.Publisher)) { UpdateBookCategories(_context, selectedCategories, bookToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } if (!ModelState.IsValid) { return(Page()); } _context.Attach(Book).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(Book.ID)) { return(NotFound()); } else { throw; } } UpdateBookCategories(_context, selectedCategories, bookToUpdate); PopulateAssignedCategoryData(_context, bookToUpdate); return(Page()); }