# 9.1: NoteEdit View

## Create the `Edit` View

Now we create the physical view for the `NoteEdit` model.

1. Still in the `NoteController.cs` file, right click on the `Edit(int id)` method
2.  Add a view as below:

    ![Edit View](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxPxfzaBtTmg_KwfdU%2F-LAxPzT-hQ0CG15Xha0_%2F9.1-A.png?generation=1524670138131187\&alt=media)

## `Edit` Post Method

1.  Back in `NoteController.cs`, build an overloaded `Edit` ActionResult under the `Edit(int id)` method:

    ```csharp
     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Edit(int id, NoteEdit model)
     {
         return View();
     }
    ```
2.  Add some validation similar to the `ActionResult Create` to make sure the `NoteId` matches:

    ```csharp
     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Edit(int id, NoteEdit model)
     {
         if(!ModelState.IsValid) return View(model);

         if(model.NoteId != id)
         {
             ModelState.AddModelError("", "Id Mismatch");
             return View(model);
         }

         return View();
     }
    ```
3.  Continue to build out the method, displaying a message to the user with the result of their actions:

    ```csharp
     [HttpPost]
     [ValidateAntiForgeryToken]
     public ActionResult Edit(int id, NoteEdit model)
     {
         if(!ModelState.IsValid) return View(model);

         if(model.NoteId != id)
         {
             ModelState.AddModelError("", "Id Mismatch");
             return View(model);
         }

         var service = CreateNoteService();

         if (service.UpdateNote(model))
         {
             TempData["SaveResult"] = "Your note was updated.";
             return RedirectToAction("Index");
         }

         ModelState.AddModelError("", "Your note could not be updated.");
         return View(model);
     }
    ```

## Remove `NoteId` from the Edit View

1. Go to **ElevenNote.Web -> Views -> Note -> Edit.cshtml**
2. Delete lines 18-24, we don't need to see the `NoteId`, and it should not be editable.
3. We will add a reference to this code, but make it hidden.
4.  Add this line of code _(starting with_ `@Html.HiddenFor...`_)_ in place of the lines we deleted:

    ```markup
     @HtmlValidationSummary(true, "", new { @class = "text-danger" })
     @Html.HiddenFor(model => model.NoteId)

     <div class="form-group">
    ```
5. Run the app and make sure you can edit a note, if so, ![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)
6.  If you run from the `Edit.cshtml` file, you will get an error because the URL goes to _/Note/Edit_ but not to a specific note id.

    ![Error](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxPxfzaBtTmg_KwfdU%2F-LAxPziGzentQZKTTvbA%2F9.1-Error.PNG?generation=1524670137050735\&alt=media)
7.  Change the URL to remove _/Edit_ to get to the index view which lists all the notes.

    ![Index](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxPxfzaBtTmg_KwfdU%2F-LAxPzjljOSDwwj1QxoF%2F9.1-Index.PNG?generation=1524670136778344\&alt=media)

[Next,](../part-10-notedelete/10.0-notedelete) we'll add the ability to delete a note.
