简体   繁体   中英

validation in model and viewmodel mvc

I have a domain model which has 10 fields in it. I have 5 views with different fields from this model(Each view having a different set of fields). For this purpose, I created a ViewModel for each view(total 5 ViewModels).

My question is in every view model I have to duplicate the validation logic. Is there any simple approach to avoid validation logic duplicating for every ViewModel?

Below is how my models and ViewModels look like.

public class Student {
    public int Id { get; set; }
    [Required]
    [StringLength(50)]
    public string Name { get; set; }
    [StringLength(15)]
    [DataType(DataType.PhoneNumber)]
    public string Mobile { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [Range(5,12)]
    public int ClassId { get; set; }
    [Range(0,1000)]
    public int MarksObtained { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime DateOfBirth { get; set; }
}


public class StudentDetailsViewModel {
    //validation duplicated for each field
    public int Id { get; set; }
    [Required]
    [StringLength(50)]
    public string Name { get; set; }
    [StringLength(15)]
    [DataType(DataType.PhoneNumber)]
    public string Mobile { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime DateOfBirth { get; set; }
}


public class StudentMarksViewModel
{
    //validation duplicated for each field
    public int Id { get; set; }
    [Required]
    [StringLength(50)]
    public string Name { get; set; }
    [Range(5, 12)]
    public int ClassId { get; set; }
    [Range(0, 1000)]
    public int MarksObtained { get; set; }
}

So I do not want my validation logic duplicated everywhere. I want a centralized validation logic and my ViewModels to use them without mentioning everywhere.

Yes.

Let every ViewModel Inherit from a BaseModel and have the validation logic there.

Your Base Model

public class VM_Student //Your Base
{
  //Only include Attributes here that you need every time.
  public int Id { get; set; }
  [Required]
  [StringLength(50)]
  public string Name { get; set; }

}

Your ViewModels

public class VM_StudentFull : VM_Student
{
  //Only Add the Extra Fields here, the StudentFull inherits
  //the other attributes and validation
  [StringLength(15)]
  [DataType(DataType.PhoneNumber)]
  public string Mobile { get; set; }
  [DataType(DataType.EmailAddress)]
  public string Email { get; set; }
  [DataType(DataType.DateTime)]
  public DateTime DateOfBirth { get; set; }          
}

public class VM_StudentMarks : VM_Student
{
  //Only Add the Extra Fields here again,
  //the StudentMarks inherits the other attributes and validation
  [Range(5,12)]
  public int ClassId { get; set; }
  [Range(0,1000)]
  public int MarksObtained { get; set; }
  [DataType(DataType.DateTime)]           
}

This is just a quick example, of course you'd need to match it to your solution accordingly. Sort out the attributes you need in every ViewModel , and plainly add them to the new ViewModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM