1
我有一种情况,我需要验证一个孩子,但只有当它存在。基本上用户可以输入银行账户或信用卡,我只想验证他们输入的。ASP.NET MVC 3复杂类型验证
下面是型号:
public class AccountViewModel
{
[Required]
public bool isBankAccount { get; set; }
[RequiredIf("isBankAccount")]
public BankAccount BankAccount { get; set; }
[RequiredIf("isBankAccount",
IfNot = true)]
public CreditCard CreditCard { get; set; }
}
public class CreditCard
{
[Required]
[CreditCard]
public string CreditCardNumber { get; set; }
[Required]
[Range(1, 12)]
public int? ExpiryMonth { get; set; }
[Required]
[Range(2000, 3000)]
public int? ExpiryYear { get; set; }
[Required]
public string CardHolderName { get; set; }
}
public class BankAccount
{
[Required]
public string BSB { get; set; }
[Required]
[StringLength(10,
MinimumLength = 3)]
[NumbersOnly]
public string AccountNumber { get; set; }
[Required]
public string AccountHolderName { get; set; }
}
我的问题是孩子们的属性仍然被验证,尽管父属性验证为真。有没有办法阻止孩子们验证父母是否这样说?
看看[使用数据注释验证相关属性](http://stackoverflow.com/questions/2280539/custom-model-validation-of-dependent-properties-using-data-annotations)。 – 2010-11-18 07:24:41