专栏首页张善友的专栏ASP.NET MVC2 数据模型验证类库:MVC Foolproof Validation

ASP.NET MVC2 数据模型验证类库:MVC Foolproof Validation

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: }

有效的操作符验证器

   1: [Is]
   2: [EqualTo]
   3: [NotEqualTo]
   4: [GreaterThan]
   5: [LessThan]
   6: [GreaterThanOrEqualTo]
   7: [LessThanOrEqualTo]

非空验证

   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: }

条件非空验证

   1: [RequiredIf]
   2: [RequiredIfNot]
   3: [RequiredIfTrue]
   4: [RequiredIfFalse]
   5: [RequiredIfEmpty]
   6: [RequiredIfNotEmpty]
   7: [RequiredIfRegExMatch]
   8: [RequiredIfNotRegExMatch]

启用客户端验证

要启用客户端验证,必须包含标准的客户端验证文件和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>

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: }

应用到模型之中:

   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: }

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制

    将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft ...

    张善友
  • 轻型的ORM类Dapper

    Dapper是一个轻型的ORM类。代码就一个SqlMapper.cs文件,主要是IDbConnection的扩展方法,编译后就40K的一个很小的dll。官方站点...

    张善友
  • ASP.NET Identity 2新增双重认证、帐号锁定、防伪印章功能并修复了一些bug

    Microsoft最近发布了ASP.NET Identity 2,该版本支持双重认证、帐号锁定以及防伪印章功能,还增强了用户帐号和索引。此外新版本还包含一个改进...

    张善友
  • 19 个强大、有趣、好玩、又装B的 Linux 命令!

    输出一句话,有笑话,名言什么的 (还有唐诗宋词sudo apt-get install fortune-zh)

    程序员小明
  • 19 个强大、有趣、又好玩的 Linux 命令!

    输出一句话,有笑话,名言什么的 (还有唐诗宋词sudo apt-get install fortune-zh)

    Spark学习技巧
  • MySQL的两个主要分支

    开始之前,我们首先截取一个当前数据库使用流行度的一个图,来简单分析下这两个分支现在的使用情况,图截取自DB-Engine网站:

    AsiaYe
  • 19 个恶搞 Linux 命令

    【导言】得益于 Linux 开放自由的生态,使得其成为开发者们首选的操作系统,今天跟大家分享几个 Linux 上比较有趣的命令,祝各位玩得开心~

    GitHubDaily
  • Linux的19 个装B的命令,这次装的可以!

    -a An accident seems to happen. You'll feel pity for people who cry for help. -...

    互扯程序
  • [日常] MySQL内存不足启动失败的解决方法

    1.启动mysql时,一直不成功,查看错误日志 /var/log/mysql/error.log

    陶士涵
  • 比 rm -rf /* 还骚的 Linux 19 个装 B 的命令!

    原文链接:http://www.cnblogs.com/sukai/archive/2013/06/08/312703...

    业余草

扫码关注云+社区

领取腾讯云代金券