MVC学习手册之数据注解与验证
MVC学习手册之数据注解与验证
新建一个MVC5的WEB应用程序,VS2013会自动生成一段代码,以下是Account控制器下Register.cshtml 页面的代码:
@model WebApplication1.Models.RegisterViewModel
@{
ViewBag.Title = "注册";
} <h2>@ViewBag.Title。</h2> @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>创建新帐户。</h4>
<hr />
@Html.ValidationSummary()
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="注册" />
</div>
</div>
} @section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6gAAAF2CAIAAADsiCIJAAAd30lEQVR4nO3dPW/iWMOA4fc/uaJKl2oqulR0qaionlSp6FzlD1Clo6KiS0VneR+5QqIgWi3akZBGeljtjrOM5LfIF9/BITOGnOuqBmObE1aR7pw9tv+vAACAAPxf1QMAAIBfQfgCABAE4QsAQBCELwAAQRC+AAAEQfgCABAE4QsAQBCELwAAQRC+HyDPd78/7l7F/eHs1wwGAICNhO/h8ixuNNrdbLp9l0mvWYvOLjvJjn22mY5Xm3nSu7rqT17eT/prp530rq/vJgUAAC+E74cYdxtRFNV3xObs7qoWRVGt2S/bvlkcRWf1i1f1syiKaq3nE83urlZ2eNqjcTt8YyoaACAgwveDDDv1KIpq7W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用HTML辅助方法,可以很方便的生成我们想要的html代码,并提供很方便的数据验证功能。这里只是简单的举一些常用的特性:
public class RegisterViewModel
{
[Required(ErrorMessage = "用户名是必须的")]
[Display(Name = "用户名")]
public string UserName { get; set; } [Required]
[StringLength(, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = )]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; } [DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
}
- Required 特性:用它标记的属性表示必须的,在数据验证时如果为空,则返回错误消息(通过 ErrorMessage 这个参数可以自定义返回的消息和消息类型)。
- Display 特性:它标记的属性,在调用Html辅助方法时会读取它的 Name 参数的值并显示。如上面的LabelFor(m=>m.UserName)它返回的就是 Name 参数的值。
- DataType 特性:它规定了所标记属性的数据显示类型, 在 DataType 这个枚举类型中定义了很多显示方式,如Password 表示以密码的方式显示,这样在用户输入密码时就不会以明文显示。
- StringLength 特性:它规定了所标记属性的的长度范围,如 Password 这个属性的最大长度为100个字符。
- Compare 特性:它修饰的属性可以与另一个属性相比较,如果它们的值相等,则返回true 否则为false ,如果为false 就返回 ErrorMessage 的值。如 ConfirmPassword 这个属性会自动与 Password 这个属性比较。
- RegularExpression 特性:正则表达式,用它修饰的属性会通过它将属性的值转换为正则表达式,在于所给定的参数比较,如果不相符,就返回 ErrorMessage 的值。
- Range 特性:它规定了所修饰的属性的值的可取值范围。如 [Range(34,43)]public int Age{get;set;} 那么Age的可取值为34到43,如果不在这个范围就返回 ErrorMessage 的值。
- Remote 特性:它所修饰的属性可以与远端服务器进行验证。如 [Remote("CheckUserName","Account")]public string UserName{get;set;} 会将UserName属性的值返回到服务器,并通过 Account 控制器下的 CheckUserName(string username)方法来进行处理。
前面讲完了一些基本的用于数据验证的特性操作(这些特性也叫做注解),那么接下来就来讲讲如何创建自定的验证操作:
自定义注解需要从 ValidationAttribute 派生自己类,并重写一个版本的 IsValid() 方法。如下面的例子创建了一个自定义的最大化的单词数的注解(特性):
public class MaxWordsAttribute : ValidationAttribute
{
private readonly int _maxWords; public MaxWordsAttribute(int maxWords):base("{0}的单词数过多!")
{
_maxWords = maxWords;
} protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var valueAsString = value as string;
if (valueAsString.Split(' ').Length > _maxWords)
{
var errorMessage = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(errorMessage);
}
}
return ValidationResult.Success;
}
}
通过空格将字符串分割为多个字符串,并计算子字符串的数目与限定的最大字符串数目比较,如果超过了限定,就返回 errorMessage 的值,在通过 FormatErrorMessage 方法重写了默认实现方式。
下面是被标记的UserName属性以及操作的结果:
[MaxWords()]
[Required(ErrorMessage = "用户名是必须的")]
[Display(Name = "用户名")]
public string UserName { get; set; }
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxcAAAGWCAIAAACFMek/AAAgAElEQVR4nO3dzWsjaYLg4fmf4qSTb0lR5KVVlQfnsuhS+NAImjFzaOfFU9C+DIICU6e8iJ0dszODdgtEHZRQvb6UDgVq1ZRIWNWKRUmT6swc0TndmsyqcDkh9uAvfYakV5Zlh58HQ5dCEaFXbhL9eON16G8SAACW9zebHgAAwJ2kogAAQqgoAIAQKgoAIISKAgAIoaIAAEKoKACAECoKACCEigIACKGiAABC3IuKiuP057uVvVKtPbiZwQAA2XAfKipulQqFg0qrP3uXXrWYi7Z2yo2UfWbpd8cDrFfd26v1Lp9v1CZO26vu7x/3EgDg7roPFZUkSbdSiKIon1Iug+O9XBRFuWJt2ZBqlaJoK799Jb8VRVFu9+JEg+O9sR3O9ygctedMkgEAt9c9qagkaZfzURTlDhozw+WstKKlM6pViqKo1BrdUKz2Zz4+27JTNRcFAHfZvamoZFDbjaKH5XaSJEm3PW0WaFDbjXJ7x5Pro/q9tLAKrKjRLQDAXXN/KiqJW0dHrThJkqR9VGlP3aPX6o43VNytFHNRvtSaOYelogDgXrpHFXWpV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WL8tncrezjkq76+boXSIC7xe1yGTSUjN5i87tiSeAey/jFXUXzfqMH54dSZkymTwwG1aJGxUFwDqoqFtkrIoWvIQ0mQjZ+3SflY+TNZn+3ie7c+6Zp54zpV8BuD9UFABACBUFABBCRQEAhFBRAAAhVBQAQAgVBQAQQkUBAIRQUQAAIVQUAEAIFQUAEEJFAQCEUFEAACFUFABACBUFABBCRQEAhFBRAAAhVBQAQAgVBQAQQkUBAIRQUQAAIVQUAEAIFQUAEEJFAQCEUFEAACFUFABACBUFABDi/wPPmZM/q5xNhQAAAABJRU5ErkJggg==" alt="" />
类似的我们可以自定义其它注解与验证方式。
参考资料: ASP.NET MVC 5 高级编程(第5版)
MVC学习手册之数据注解与验证的更多相关文章
- 【ASP.NET MVC系列】浅谈数据注解和验证
[ASP.NET MVC系列]浅谈数据注解和验证 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google C ...
- MVC中的数据注解和验证
数据注解和验证 用户输入验证在客户端浏览器中需要执行验证逻辑. 在客户端也需要执行. 注解是一种通用机制, 可以用来向框架注入元数据, 同时, 框架不只驱动元数据的验证, 还可以在生成显示和编辑模型的 ...
- asp.net mvc常用的数据注解和验证以及entity framework数据映射
终于有时间整理一下asp.net mvc 和 entity framework 方面的素材了. 闲话少说,步入正题: 下面是model层的管理员信息表,也是大伙比较常用到的,看看下面的代码大伙应该不会 ...
- APS.NET MVC + EF (08)---数据注解和验证
对于Web开发人员来说,用户输入验证一直是一个挑战.不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.如果觉得验证是令人望而生畏的繁杂琐事,ASP.NET MVC框架提供了数据注解的方式帮助 ...
- 【第二篇】ASP.NET MVC快速入门之数据注解(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- MVC5 数据注解和验证
①利用数据注解进行验证 ②创建自定义的验证逻辑 ③模型元数据注解的用法 ①先创建数据源 1,创建我们的Model Order 2,创建控制器带EF 选择模型为Order 当你运行的时候会报错,需要代 ...
- Asp.net MVC 数据注解与验证
数据注解特性定义在名称空间System.ComponentModel.DataAnnotations中(有些特性定义在其他名称空间中),它们提供了服务器端验证的功能,当在模型的属性上使用这些特性时,框 ...
- 数据注解和验证 – ASP.NET MVC 4 系列
不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...
- ASP.NET MVC5----常见的数据注解和验证
只要一直走,慢点又何妨. 在使用MVC模式进行开发时,数据注解是经常使用的(模型之上操作),下面是我看书整理的一些常见的用法. 什么是验证,数据注解 验证 从全局来看,发现逻辑仅是整个验证的很小的一部 ...
随机推荐
- Glib之主事件循环
介绍 GLib和GTK+应用的主事件循环管理着所有事件源.这些事件的来源有很多种比如文件描述符(文件.管道或套接字)或超时.新类型的事件源可以通过g_source_attach()函数添加. 为了让多 ...
- Python3中的Json与Pickle
用于将数据存取到文件中: 1:Json的存数据到文件: import json info = { "name":"hjc", "age":2 ...
- forEach,for in,for of循环的用法
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- 安装Scrapy报错 error: Microsoft Visual C++ 14.0 is required解决方法
[问题背景]:在Windows 10系统,pip install Scrapy,报错error: Microsoft Visual C++ 14.0 is required,还有提示Twisted需要 ...
- 「NOI.AC」Leaves 线段树合并
题目描述 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为\(1\dots n\)的一个排列).可以任意交换每个非叶子节点的左右孩子. ...
- POJ1063 Flip and Shift
题目来源:http://poj.org/problem?id=1063 题目大意: 有一种游戏如图所示.一个填满黑白球的转盘,它可以有两种操作,一种是将大转盘顺时针旋转,所有球的位置顺时针挪一位,另一 ...
- ubuntu 16 64位编译安装php
./configure \ --prefix=/usr/local/php7 \ --exec-prefix=/usr/local/php7 \ --with-config-file-path=/us ...
- P1060 开心的金明(动态规划背包问题)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱 ...
- POJ3041轰炸行星(匈牙利算法 最小覆盖点集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 13625 Descr ...
- poj1062 昂贵的礼物(dijkstra+枚举)
传送门:点击打开链接 题目大意:买东西,每个东西有了替代品,拥有替代品后可以有优惠价格,每个物品的主人有自己的等级,等级超过m的不能直接或者间接交易,问买1号物品的最低价格是多少. 思路:一开始想到d ...