5.3: Change NoteCreate Model

In this module we'll override the ToString() method in our models.

Alter ToString() override

In this section, we alter the ToString() override with a fat arrow function, also known as a lambda expression. Lambda expressions allow you to write local functions that can be passed as arguments or returned by function calls. In this case, we use it with the ToString() method to convert Title to a string regardless of its data type.

  1. Go back to the NoteCreate.cs model

  2. Change the ToString() override to match this:

             [MaxLength(8000)]
             public string Content { get; set; }  
    
             public override string ToString() => Title;
  3. Do the same for the NoteListItem.cs model

         namespace ElevenNote.Models
         {
             public class NoteListItem
             {
                 public int NoteId { get; set; }
                 public string Title { get; set; }    
    
                 [Display(Name="Created")]
                 public DateTimeOffset CreatedUtc { get; set; }
    
                 public override string ToString() => Title;
             }
         }
  4. Git

Last updated