Validator.TryValidateProperty 不起作用
我正在尝试实现 Validator.TryValidateProperty,即使有 [Required] DataAnnotation,TryValidateProperty 也会返回有效响应。
这是我的 Customer 部分类:
[MetadataType(typeof(Customer.Metadata))]
public partial class Customer : global::System.Data.Objects.DataClasses.EntityObject
{
...
private sealed class Metadata
{
[Required]
[SSNValidAttribute(ErrorMessage = "The SSN should be 9 numeric characters without any punctuation.")]
[DisplayName("SSN")]
public String SSN { get; set; }
...
这是返回 True 的代码:
...
var customer = new Customer();
customer.SSN = "";
var vc = new ValidationContext(customer, null, null);
vc.MemberName = "SSN";
var res = new List<ValidationResult>();
var result = Validator.TryValidateProperty(customer.SSN, vc, res);
...
I am trying to implement Validator.TryValidateProperty and even though there is a [Required] DataAnnotation, the TryValidateProperty returns a valid response.
Here is my Customer partial class:
[MetadataType(typeof(Customer.Metadata))]
public partial class Customer : global::System.Data.Objects.DataClasses.EntityObject
{
...
private sealed class Metadata
{
[Required]
[SSNValidAttribute(ErrorMessage = "The SSN should be 9 numeric characters without any punctuation.")]
[DisplayName("SSN")]
public String SSN { get; set; }
...
And here is the code that is returning True:
...
var customer = new Customer();
customer.SSN = "";
var vc = new ValidationContext(customer, null, null);
vc.MemberName = "SSN";
var res = new List<ValidationResult>();
var result = Validator.TryValidateProperty(customer.SSN, vc, res);
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,刚刚找到了处理密封
MetadataType
类的解决方案。我必须添加以下内容:
在此地址找到解决方案:
http://forums.silverlight.net/forums/p/149264/333396。 ASPX
Ok, just found the solution for dealing with the sealed
MetadataType
class.I had to add the following:
Found solution at this address:
http://forums.silverlight.net/forums/p/149264/333396.aspx