I have a model that contains basic information. However, my View requires more information just for display so I think that a ViewModel is needed here to display that extra information. However, should I add the Validation attributes in the model so that when I perform Code-First migration, it automatically creates the database for me with the correct datatype of each columns or should I add the Validation attributes to the ViewModel since the form should validate the filled information?
public class Module
{
[Key]
public int id { get; set; }
[Required]
[StringLength(100)]
[Column(TypeName = "varchar")]
[Display(Name="Module Name")]
public string ModuleName { get; set; }
}
public class ModuleViewModel
{
[Key]
public int id { get; set; }
[Required]
[StringLength(30)]
[Column(TypeName="varchar")]
[Display(Name="Module ID")]
public string ModuleID { get; set; }
[Required]
[StringLength(100)]
[Column(TypeName = "varchar")]
[Display(Name="Module Name")]
public string ModuleName { get; set; }
//To populate dropdownlist
public List<SelectListItem> ModuleLevelList { get; set; }
}
Do I really need a ViewModel in this case?
Moduleclass is your entity class, it doesn't make any sense to put validation into it, you can use viewmodel for validation and other stuff to display on view page, In that case you will need to map your entity classes with your viewmodel classes.