6.1: NoteController Changes
Make Changes in the NoteController.cs
NoteController.csIn this module, we will be adding code to the skeleton NoteController we previously made.
Open the
NoteController.csfileIn 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); }CTRL .to bring in the using statementsThe
Index()method displays all the notes for the current user. Notice the methods and services that it calls upon.Add the following code in the
Createmethod[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"); } } }The
Create(NoteCreate model)method makes sure the model is valid, grabs the currentuserId, calls onCreateNote, and returns the user back to the index view.
Next, we'll set some breakpoints and look at the database.
Last updated