public async Task <IActionResult> DeleteConfirmed(BlogDeleteViewModel viewModel) { /* * We want to retrieve the post from the database. * We will utilise the blogs id within the view model. */ try { /* * Instead of retrieving the record from the database, * We can directly remove it from the database. */ _DB.Posts.Remove(viewModel.Post); /* * We then save the changes to the database */ await _DB.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } /* * Then redirect the user back to the Index function */ return(RedirectToAction(nameof(Index))); }
// GET: Blog/Delete/5 public async Task <IActionResult> Delete(int?id) { /* * We check to make sure an id has been passed. * Else we want to return the not found. */ if (id == null) { return(NotFound()); } /* * Then we setup the blog delete view model. */ BlogDeleteViewModel viewModel = new BlogDeleteViewModel() { Post = await _DB.Posts.FirstOrDefaultAsync(m => m.Id == id) }; /* * Double check the post query didn't return null * If it does we want to return not found. */ if (viewModel.Post == null) { return(NotFound()); } return(View(viewModel)); }