5.1: NoteCreate Model

In this section, we'll create the NoteCreate model that allow us to have some validation for the note properties.

Purpose

Think about the things we'll need to capture when we create a note and save it to the database. We need to create a Title, Content, and DateCreated. Will we provide an id though?

The answer is no. The id will be created after the POST request happens, after we fill out a form with the two properties above. So, we won't provide that. Our .Service and .Data layer will work together to take care of that. Let's talk about that again soon.

NoteCreate model

  1. Right click on ElevenNote.Models

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

  3. Make the class public.

  4. Add the following properties (for the ToString method, you can type override and then a space):

namespace ElevenNote.Models
{
    public class NoteCreate
    {
        public string Title { get; set; }
        public string Content { get; set; }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}
  1. Add the [Required] annotation to the Title property:

             [Required]
             public string Title { get; set; }
             public string Content { get; set; }
  2. CTRL . to bring in the using statement

  3. We can also add annotations to require or limit the number of characters in each field.

             [Required]
             [MinLength(2, ErrorMessage = "Please enter at least 2 characters.")]
             [MaxLength(100, ErrorMessage = "There are too many characters in this field.")]
             public string Title { get; set; }
    
             [MaxLength(8000)]
             public string Content { get; set; }
  4. Next, we'll make the view for creating a note.

Last updated