buddy-class

DataAnnotation attributes buddy class strangeness - ASP.NET MVC

∥☆過路亽.° 提交于 2019-12-06 05:39:48
问题 Given this POCO class that was automatically generated by an EntityFramework T4 template (has not and can not be manually edited in any way): public partial class Customer { [Required] [StringLength(20, ErrorMessage = "Customer Number - Please enter no more than 20 characters.")] [DisplayName("Customer Number")] public virtual string CustomerNumber { get;set; } [Required] [StringLength(10, ErrorMessage = "ACNumber - Please enter no more than 10 characters.")] [DisplayName("ACNumber")] public

Validation does not work when I use Validator.TryValidateObject

痞子三分冷 提交于 2019-11-27 20:50:26
DataAnnotations does not work with buddy class. The following code always validate true. Why ? var isValid = Validator.TryValidateObject(new Customer(), Context, results, true); and here is the buddy class. public partial class Customer { public string Name { get; set; } public int Age { get; set; } } [MetadataType(typeof(CustomerMetaData))] public partial class Customer { public class CustomerMetaData { [Required(ErrorMessage = "You must supply a name for a customer.")] public string Name { get; set; } } } Here is another thread with same question., but no answer. link text I found the answer

Validation does not work when I use Validator.TryValidateObject

淺唱寂寞╮ 提交于 2019-11-26 18:20:49
问题 DataAnnotations does not work with buddy class. The following code always validate true. Why ? var isValid = Validator.TryValidateObject(new Customer(), Context, results, true); and here is the buddy class. public partial class Customer { public string Name { get; set; } public int Age { get; set; } } [MetadataType(typeof(CustomerMetaData))] public partial class Customer { public class CustomerMetaData { [Required(ErrorMessage = "You must supply a name for a customer.")] public string Name {