8.0: NoteDetail Model
Create the NoteDetail
Model
NoteDetail
ModelIn this section, we write the NoteDetail
model. This will let us view all the properties of the note.
In the Solution Explorer, right click on ElevenNote.Models
Select Add -> Class and name it
NoteDetail.cs
Make the class public
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}"; } }
Add data annotations:
[Display(Name="Created")] public DateTimeOffset CreatedUtc { get; set; } [Display(Name="Modified")] public DateTimeOffset? ModifiedUtc { get; set; }
CTRL .
to bring in the using statement for the annotations
Details()
Method
Details()
MethodOpen
NoteController.cs
Create a
Details()
method under theCreate(NoteCreate model)
but above theprivate NoteService CreateNoteService()
:public ActionResult Details(int id) { var svc = CreateNoteService(); var model = svc.GetNoteById(id); return View(model); }
Ignore the error for now.
GetNoteById()
Method
GetNoteById()
MethodOpen
NoteService.cs
Add a
GetNoteById()
method under theGetNotes()
method:public NoteDetail GetNoteById(int id) { }
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