8.0: NoteDetail Model

Create the NoteDetail Model

In this section, we write the NoteDetail model. This will let us view all the properties of the note.

  1. In the Solution Explorer, right click on ElevenNote.Models

  2. Select Add -> Class and name it NoteDetail.cs

  3. Make the class public

  4. Add the following properties:

     namespace ElevenNote.Models
     {
         public class NoteDetail
         {
             public int NoteId { get; set; }
             public string Title { get; set; }
             public string Content { get; set; }
             public DateTimeOffset CreatedUtc { get; set; }
             public DateTimeOffset? ModifiedUtc { get; set; }
             public override string ToString() => $"[{NoteId}] {Title}";
         }
     }
  5. Add data annotations:

     [Display(Name="Created")]
     public DateTimeOffset CreatedUtc { get; set; }
    
     [Display(Name="Modified")]
     public DateTimeOffset? ModifiedUtc { get; set; }
  6. CTRL . to bring in the using statement for the annotations

Details() Method

  1. Open NoteController.cs

  2. Create a Details() method under the Create(NoteCreate model) but above the private NoteService CreateNoteService():

     public ActionResult Details(int id)
     {
         var svc = CreateNoteService();
         var model = svc.GetNoteById(id);
    
         return View(model);
     }
  3. Ignore the error for now.

GetNoteById() Method

  1. Open NoteService.cs

  2. Add a GetNoteById() method under the GetNotes() method:

     public NoteDetail GetNoteById(int id)
     {
    
     }
  3. Add a using statement to connect to the database ApplicationDbContext():

     public NoteDetail GetNoteById(int noteId)
     {
         using (var ctx = new ApplicationDbContext())
         {
             var entity = 
                 ctx
                     .Notes
                     .Single(e => e.NoteId == noteId && e.OwnerId == _userId);
                 return
                     new NoteDetail
                     {
                         NoteId = entity.NoteId,
                         Title = entity.Title,
                         Content = entity.Content,
                         CreatedUtc = entity.CreatedUtc,
                         ModifiedUtc = entity.ModifiedUtc
                     };
         }
     }

Next, we'll create the view for the note detail page.

Last updated