6.1: NoteController Changes

Make Changes in the NoteController.cs

In this module, we will be adding code to the skeleton NoteController we previously made.

  1. Open the NoteController.cs file

  2. In the Index() method change the following:

     public ActionResult Index()
     {
         var userId = Guid.Parse(User.Identity.GetUserId());
         var service = new NoteService(userId);
         var model = service.GetNotes();
    
         return View(model);
     }
  3. CTRL . to bring in the using statements

  4. The Index() method displays all the notes for the current user. Notice the methods and services that it calls upon.

  5. Add the following code in the Create method

             [HttpPost]
             [ValidateAntiForgeryToken]
             public ActionResult Create(NoteCreate model)
             {
                 if (!ModelState.IsValid)
                 {
                     return View(model);
                 }
    
                 var userId = Guid.Parse(User.Identity.GetUserId());
                 var service = new NoteService(userId);
    
                 service.CreateNote(model);
    
                 return RedirectToAction("Index");
             }
         }
     }
  6. The Create(NoteCreate model) method makes sure the model is valid, grabs the current userId, calls on CreateNote, and returns the user back to the index view.

Next, we'll set some breakpoints and look at the database.

Last updated