Validation in Model or ViewModel

Supposing I have Model Course

//A few of the attributes

 [Key] public int CourseId { get; set; } [Required(ErrorMessage = "Course title is required")] [StringLength(100, MinimumLength = 3, ErrorMessage = "Course title must be between {2} and {1} characters long")] [DataType(DataType.Text)] [Display(Name = "Course Title")] public string CourseTitle { get; set; } [Required(ErrorMessage = "Course capacity is required")] [Display(Name = "Available places on course")] public int CourseCapacity { get; set; } 

Would I replicate this exactly in an AddCourseViewModel with the same Data Annotations?

My understanding is the ViewModel will pass the data to the Model (I plan on using AutoMapper), if the data is validated on the ViewModel does it still need validated again when its passed to the Model?

Also if no validation happens on the ViewModel that would mean someone could leave a required field empty? right?

submitted by /u/Subotai__
[link] [comments]

Leave a Reply