MVC Foolproof Validation是一个数据模型类库扩展。 操作符验证 有效的操作符验证器 非空验证 条件非空验证 启用客户端验证要启用客户端验证,必须包含标准的客户端验证文件和Mvc...
MVC Foolproof Validation是一个数据模型类库扩展。
操作符验证
1: public class SignUpViewModel
2: {
3: [Required]
4: public string Password { get; set; }
5:
6: [EqualTo("Password", ErrorMessage="Passwords do not match.")]
7: public string RetypePassword { get; set; }
8: }
9: public class EventViewModel
10: {
11: [Required]
12: public string Name { get; set; }
13:
14: [Required]
15: public DateTime Start { get; set; }
16:
17: [Required]
18: [GreaterThan("Start")]
19: public DateTime End { get; set; }
20: }
2: {
有效的操作符验证器
1: [Is]
2: [EqualTo]
3: [NotEqualTo]
4: [GreaterThan]
5: [LessThan]
6: [GreaterThanOrEqualTo]
7: [LessThanOrEqualTo]
2: [EqualTo]
非空验证
1: private class Person
2: {
3: [Required]
4: public string FirstName { get; set; }
5:
6: [Required]
7: public string LastName { get; set; }
8:
9: public bool Married { get; set; }
10:
11: [RequiredIfTrue("Married")]
12: public string MaidenName { get; set; }
13: }
2: {
条件非空验证
1: [RequiredIf]
2: [RequiredIfNot]
3: [RequiredIfTrue]
4: [RequiredIfFalse]
5: [RequiredIfEmpty]
6: [RequiredIfNotEmpty]
7: [RequiredIfRegExMatch]
8: [RequiredIfNotRegExMatch]
2: [RequiredIfNot]
启用客户端验证
要启用客户端验证,必须包含标准的客户端验证文件和MvcFoolproofValidation.js文件:
1: <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
2: <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
3: <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
4: <script src="../../Scripts/MvcFoolproofValidation.js" type="text/javascript"></script>
2: <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
jQuery验证
如果使用jQuery验证,则必须包含标准的客户端验证文件和MvcFoolproofJQueryValidation.js文件:
1: <script src="../../Scripts/jquery.js" type="text/javascript"></script>
2: <script src="../../Scripts/jquery-validate.js" type="text/javascript"></script>
3: <script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>
4: <script src="../../Scripts/MvcFoolproofJQueryValidation.js" type="text/javascript"></script>
复杂自定义验证
自定义验证属性:
1: public class RoleValidInDepartmentAttribute : ModelAwareValidationAttribute
2: {
3: //this is needed to register this attribute with foolproof's validator adapter
4: static RoleValidInDepartmentAttribute() { Register.Attribute(typeof(RoleValidInDepartmentAttribute)); }
5:
6: public override bool IsValid(object value, object container)
7: {
8: if (value != null && value.ToString() == "Software Developers")
9: {
10: //if the role was software developers, we need to make sure the user is in the IT department
11: var model = (CreateUserViewModel)container;
12: return model.Department == "IT Department";
13: }
14:
15: //the user wasn't in a constrained role, so just return true
16: return true;
17: }
18: }
2: {
3: //this is needed to register this attribute with foolproof's validator adapter
应用到模型之中:
1: public class CreateUserViewModel
2: {
3: [Required]
4: public string Username { get; set; }
5:
6: [Required]
7: public string Department { get; set; }
8:
9: [Required]
10: [RoleValidInDepartment(ErrorMessage="This role isn't valid for the selected department.")]
11: public string Role { get; set; }
12: }
2: {
Foolproof Provides Contingent Data Annotation Validation for ASP.NET MVC 2
Build Model-Aware Custom Validation Attributes in ASP.NET MVC 2
Client Side Model-Aware Validation
官方地址:http://foolproof.codeplex.com
标签:类库,ASP,string,get,验证,set,Required,public,数据模型 From: https://blog.51cto.com/shanyou/7372879