// GET: Authors/Delete/5 public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } var author = await _context.Author .Include(a => a.Books) .FirstOrDefaultAsync(m => m.Id == id); if (author == null) { return(NotFound()); } // NOTE: If the author has NO books, we can delete them. // If the author has some books, we cannot delete them. var viewModel = new AuthorDeleteViewModel { Author = author, CanDelete = author.Books.Count == 0 }; return(View(viewModel)); }
public IActionResult Delete(int id) { Author authorFromDb = _context.GetPodcastAuthor(id); AuthorDeleteViewModel authorToDelete = new AuthorDeleteViewModel() { Id = id, LastName = authorFromDb.Name, FirstName = authorFromDb.FirstName }; return(View(authorToDelete)); }
public async Task <IActionResult> Delete(AuthorDeleteViewModel viewModel) { var model = await _context.Authors .Include(m => m.BookAuthors) .FirstOrDefaultAsync(x => x.Id == viewModel.Id); if (model == null) { return(NotFound()); } _context.Authors.Remove(model); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
/*[HttpPost, ActionName("Edit")] * [ValidateAntiForgeryToken] * public async Task<IActionResult> Edit(int id, AuthorEditViewModel viewModel) * { * *//*var author = await _context.Author * .Include(a => a.Books) * .FirstOrDefaultAsync(a => a.AuthorId == id);*//* * var author = viewModel.Author; * * if (id != author.AuthorId) * { * return NotFound(); * } * if (author == null) * { * return NotFound(); * } * if (author.Books.Count > 0) * { * return Forbid(); * } * else * { * * ModelState.Remove("FirstName"); * ModelState.Remove("LastName"); * * if (ModelState.IsValid) * { * _context.Update(author); * await _context.SaveChangesAsync(); *//*try * { * _context.Update(author); * await _context.SaveChangesAsync(); * } * catch (DbUpdateConcurrencyException) * { * if (id != author.AuthorId) * { * return NotFound(); * } * if (!AuthorExists(author.AuthorId)) * { * return NotFound(); * } * else * { * throw; * } * }*/ /*return RedirectToAction(nameof(Index));*//* * } * return View(viewModel); * } * }*/ /*// GET: Authors/Delete/5 * public async Task<IActionResult> Delete(int? id) * { * if (id == null) * { * return NotFound(); * } * * var author = await _context.Author * .FirstOrDefaultAsync(m => m.AuthorId == id); * if (author == null) * { * return NotFound(); * } * * return View(author); * } * * // POST: Authors/Delete/5 * [HttpPost, ActionName("Delete")] * [ValidateAntiForgeryToken] * public async Task<IActionResult> DeleteConfirmed(int id) * { * var author = await _context.Author.FindAsync(id); * _context.Author.Remove(author); * await _context.SaveChangesAsync(); * return RedirectToAction(nameof(Index)); * }*/ /*get Delete*/ public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } var author = await _context.Author .Include(a => a.Books) .FirstOrDefaultAsync(m => m.AuthorId == id); if (author == null) { return(NotFound()); } var viewModel = new AuthorDeleteViewModel { Author = author, CanDelete = author.Books.Count == 0 }; return(View(viewModel)); }