我使用的例子在The Complete Guide To Validation In ASP.NET MVC 3创建RequiredIf验证特性(这是约1/3在网页的“一个更复杂的标题下定制验证器“)。除了一个场景外,这一切都可以正常工作,这就是说,如果我需要针对复杂类型进行验证。例如,我有以下型号:获取从ModelClientValidationRequiredIfRule方法在自定义复杂类型的全名ValidationAttribute
public class MemberDetailModel
{
public int MemberId { get; set; }
// Other model properties here
public MemberAddressModel HomeAddress { get; set; }
public MemberAddressModel WorkAddress { get; set; }
}
public class MemberAddressModel
{
public bool DontUse { get; set; }
// Other model properties here
[RequiredIf("DontUse", Comparison.IsEqualTo, false)]
public string StreetAddress1 { get; set; }
}
的问题是,当该物业的StreetAddress属性验证呈现,得到它的装饰与data-val-requiredif-other="DontUse"
属性。不幸的是,由于该地址是主模型的子类型,因此需要用名称HomeAddress_Dont使用而不仅仅是Dont使用来装饰。
非常奇怪的是,验证对服务器端验证工作正常,但客户端不显眼的验证失败,JS错误,因为JS无法找到名为“DontUse”的对象。
所以,我需要找到一个方法来改变ModelClientValidationRequiredIfRule方法知道它是验证属性是子类型父类型的,如果是这样,在前面加上了ParentType_
的“otherProperty”字段(如otherProperty成为HomeAddress_DontUse
。
我曾尝试在typeof(MemberAddressModel)
传递作为属性的参数,但是在调试的属性创建即使,我似乎无法找到由于该类型是homeAddress或招聘人数的父类的任何引用。
感谢信息。我看到MS在jquery.validate.unobstrusive.js中使用的函数,但我不太清楚如何在我的JS文件中实现它们。如果你有机会,任何帮助将不胜感激,否则,我会继续玩弄弄清楚。 – bigmac 2012-04-19 22:55:17