# 10.0: NoteDelete

In this section, we'll write the code to get rid of a note.

## Delete GET Method and View

1. Open `NoteController.cs`
2. Copy the `Details(int id)` method
3. Paste it underneath the `Edit(int id, NoteEdit model)` method
4. Change the name to `Delete`

   ```csharp
        [ActionName("Delete")]
        public ActionResult Delete(int id)
        {
            var svc = CreateNoteService();
            var model = svc.GetNoteById(id);

            return View(model);
        }
   ```
5. Right click on `Delete(int id)` and add a view:

   ![Delete View](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxQg9xiPwY69w28ZXV%2F-LAxQj6YY2GVyVUiXhxZ%2F10.0-A.png?generation=1524670332844216\&alt=media)

## Delete POST Method

1. Back in `NoteController.cs`, create an `ActionResult` to remove the note from the database
2. This will need a different name due to overloading issues

   ```csharp
    [HttpPost]
    [ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeletePost(int id)
    {
        return RedirectToAction ("Index");
    }
   ```
3. Add some validation to the code:

   ```csharp
    [HttpPost]
    [ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeletePost(int id)
    {
        var service = CreateNoteService();

        service.DeleteNote(id);

        TempData["SaveResult"] = "Your note was deleted";

        return RedirectToAction ("Index");
    }
   ```
4. Ignore the error for now, it will go away once we add the code below in the `NoteService`

## NoteService

1. Open `NoteService.cs`
2. Create a `Delete` method under the `UpdateNote` method:

   ```csharp
    public bool DeleteNote(int noteId)
    {
        using (var ctx = new ApplicationDbContext())
        {
            var entity = 
                ctx
                    .Notes
                    .Single(e => e.NoteId == noteId && e.OwnerId == _userId);

            ctx.Notes.Remove(entity);

            return ctx.SaveChanges() == 1;
        }
    }
   ```
3. Run the app and make sure you can delete a note

   ![Delete](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxQg9xiPwY69w28ZXV%2F-LAxQjG3bInzfxf1J-yG%2F10.0-B.png?generation=1524670332023556\&alt=media)
4. You should get a message that your note was deleted

   ![Delete Message](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxQg9xiPwY69w28ZXV%2F-LAxQjHa6ZfkfE2lhUXV%2F10.0-C.png?generation=1524670332367912\&alt=media)
5. Open up your `dbo.Note` to confirm that the specified note was deleted.

   **Before**

   ![Before](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxQg9xiPwY69w28ZXV%2F-LAxQjJLuz_rdgdVrSbz%2F10.0-Before.PNG?generation=1524670331921205\&alt=media)

   **After**

   ![After](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxQg9xiPwY69w28ZXV%2F-LAxQjLLGhCQqPueT5Jr%2F10.0-After.PNG?generation=1524670332814048\&alt=media)
6. ![Git](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAwyv0-ttugakHO6zeV%2F-LAwyxUd0dcYSLXYb-5s%2Fdevicons_github_badge.png?generation=1524662787257488\&alt=media)

[Next,](https://eleven-fifty-academy.gitbook.io/dotnet-201-elevennote/elevennote-parts-1-11/part-11-dateformat/11.0-dateformat) we'll change the date/time format
