5

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?

3
  • I guess it's both way ... Commented Dec 5, 2016 at 17:48
  • Module class 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. Commented Dec 5, 2016 at 17:57
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. Commented Dec 5, 2016 at 21:59

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your feedback as well~!
3

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; }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.