Where do you put your validation in asp.net mvc 3?

后端 未结 5 1187
逝去的感伤
逝去的感伤 2021-02-03 15:26

One common recommended practice in asp.net mvc is that you should not send your business models to your views.. instead you should create viewmodels specific to each view.

5条回答
  •  我在风中等你
    2021-02-03 15:57

    The MetaData "buddy" class is exactly what this is for. The validation is created once but can be used on both the model and the viewmodel classes:

    public class PersonMetaData
    {
      [Required] 
      public string Name {get; set;}  
    
      [Required] 
      public string LastName {get; set;} 
    }
    
    [MetadataType(typeof(PersonMetaData))]
    public class Person
    {
      public string Name {get; set;}  
      public string LastName {get; set;} 
    }
    
    [MetadataType(typeof(PersonMetaData))]
    public class PersonFormViewModel 
    {
      public string Name {get; set;}  
      public string LastName {get; set;} 
    }
    

提交回复
热议问题