如何使用依赖字段的验证在MVC3 [英] how can i use dependency fields validation in MVC3

查看:110
本文介绍了如何使用依赖字段的验证在MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一类的字段验证属性,有这样的要求:
如果场'一'是验证成功,然后处理领域'B',但如果字段'A'是不是扔验证,忽略场B的验证确认。结果
它是可行的或者我应该想其他办法吗?

I use the validation attribute on the fields of a class and has a requirement like that: if field 'a' is validate succeed then process the validation of field 'b' but if the field 'a' is not throw validation , ignore the validation of field 'b'.
does it feasible or i should think in other way?

public class myclass
{
[required]
public string a{get;set;}
[required]
public string b{get;set;]
}

我想:
1.如果一个通过验证,然后执行乙方的验证
2.如果一个没有通过验证,那么不执行乙方的验证

i want: 1.if a pass the validate, then execute b 's validate 2.if a not pass the validate , then don't execute the b 's validate

推荐答案

退房MVC.ValidationToolkit

Check out the MVC.ValidationToolkit

<一个href=\"http://blogs.msdn.com/b/simonince/archive/2011/09/29/mvc-validationtookit-alpha-release-conditional-validation-with-mvc-3.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/simonince/archive/2011/09/29/mvc-validationtookit-alpha-release-conditional-validation-with-mvc-3.aspx

它包含了这两个验证,这将帮助你。

It contains these two validations that will help you out.


  • RequiredIfAttribute。此属性说这个领域是必需的,如果一些其他字段值X。它被用作[RequiredIf(OtherField,TargetValue)]

  • RequiredEmptyIfAttribute。此属性说如果一些其他字段值X这一领域必须是空的。它被用作[RequiredEmptyIf(OtherField,TargetValue)]

这篇关于如何使用依赖字段的验证在MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆