I have DTOs that are mapped to ViewModels. To avoid the need to use validation attributes (and other attributes), I wanted to write validation attributes for all properties in the same class and reuse them in my ViewModels. However, when I try to use metadata in a ViewModel that does not have all the DTO properties (all of them really ...), it gives me an exception System.InvalidOperationException.
An exception:
Le type de métadonnées associé pour le type 'MyProject.EntityViewModel' contient les propriétés ou champs inconnus suivants : AnotherProperty. Vérifiez que les noms de ces membres correspondent aux noms des propriétés du type principal.
Google:
The type associated metadata for type 'MyProject.EntityViewModel' contains the following unknown properties or fields: AnotherProperty. Verify that the names of these members match the names of the properties of the main type.
A simplified example:
public class Entity {
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
public class EntityDTO {
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
public class EntityInputValidation {
[Required]
public string A { get; set; }
[Required]
public string B { get; set; }
}
[MetadataType(typeof(EntityInputValidation))]
public class EntityCreateViewModel {
public string A { get; set; }
[Required]
public string C { get; set; }
}
Since EntityViewModel does not have AnotherProperty, it throws an exception. Is there any way to prevent this?
source
share