Pow4Pow5
Pow4Pow5

Reputation: 501

ASP.NET MVC Validation in ViewModel or Model?

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?

Upvotes: 5

Views: 2516

Answers (2)

Julian Wilson
Julian Wilson

Reputation: 2470

I highly recommend you use a view model. You may think it is redundant right now but I guarantee you that it is very useful and down the road you will thank me. I've been burned many times in the past trying to just use a model object everywhere and relying heavily on data annotations like yourself. Plus, you don't have to litter your model layer with view layer garbage such as [Display(Name="Module Name")]

In your case, I suggest this:

public class Module
{
[Key]
public int id { get; set; }

[Required]
[StringLength(100)]
[Column(TypeName = "varchar")]
public string ModuleName { get; set; }
}


public class ModuleViewModel
{
public int id { get; set; }

[Required]
[StringLength(30)]
[Display(Name="Module ID")]
public string ModuleID { get; set; }

[Required]
[StringLength(100)]
[Display(Name="Module Name")]
public string ModuleName { get; set; }

//To populate dropdownlist 
public List<SelectListItem> ModuleLevelList { get; set; }

}

Upvotes: 3

Brian Ogden
Brian Ogden

Reputation: 19212

Data Annotation attributes for user input validation go on the ViewModel. Data Annotations for Entity Framework Code First go on the Model.

They are conceptually two different things, validation of input and database generation using EF Code First.

For example, Required and StringLength for Entity Framework Code First creates a database column of type varchar(length) NOT NULL. Required and StringLength on the ViewModel are used in Validation of user input. Do not conflate the two, there is nothing wrong with using StringLength(length) twice. Put the length value in a static Constant if you want the length expressed one place only.

Upvotes: 6

Related Questions