MVC数据验证Model Validation
Required必须项验证属性
[Required]
public string FirstName { get; set; }
//ID编号
[ScaffoldColumn(false)]
[Required(AllowEmptyStrings = false, ErrorMessage = "用户ID不能为空")]
[Display(Name = "记录编号", Order = )]
public int ID { get; set; }
StringLength长度
[Required]
[StringLength()]
public string LastName { get; set; }
[Required]
[StringLength(, MinimumLength=)]
public string FirstName { get; set; }
RegularExpression正则表达式
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "邮箱必填")]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9]+\.[A-Za-z]{2,4}", ErrorMessage = "{0}的格式不正确")]
public string Email { get; set; }
匹配验证
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
[Compare("Email")]
public string EmailConfirm { get; set; }
Compare 比较两个字段值是否相同。
Range数字范围
[Range(,)]
public int Age { get; set; }
[Range(typeof(decimal), "0.00", "49.99")]
public decimal Price { get; set; }
Custom Error Messages and Localization自定义错误消息和本地化
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
ErrorMessage="Email doesn't look like a valid email address.")]
public string Email { get; set; }
[Required(ErrorMessage="Your last name is required")]
[StringLength(, ErrorMessage="Your last name is too long")]
public string LastName { get; set; }
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
Display
[Required]
[StringLength(, MinimumLength=)]
[Display(Name="First Name")]
public string FirstName { get; set; }
[Display(Name = "身份证号码")]
[RegularExpression(@"\d{17}[\d|x]|\d{15}", ErrorMessage = "身份证号码格式错误")]
<div class="editor-label">
@Html.LabelFor(t => t.IdentityNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IdentityNo)
@Html.ValidationMessageFor(model => model.IdentityNo)
</div>
自动生成编辑页面
<fieldset>
<legend>Shipping Information</legend>
@Html.EditorForModel()
</fieldset>
<div>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>UserInfo</legend>
<div class="editor-label">
@Html.LabelFor(t => t.UserPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserPassword)
@Html.ValidationMessageFor(model => model.UserPassword)
</div>
<div class="editor-label">
@Html.LabelFor(t => t.IdentityNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IdentityNo)
@Html.ValidationMessageFor(model => model.IdentityNo)
</div>
<div class="editor-label">
@Html.LabelFor(t => t.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(t => t.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(t => t.Money)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Money)
@Html.ValidationMessageFor(model => model.Money)
</div>
<div class="editor-label">
@Html.LabelFor(t => t.TEmail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TEmail)
@Html.ValidationMessageFor(model => model.TEmail)
</div>
@Html.EditorForModel()
</fieldset>
<input type="submit" value="提交" />
}
</div>
隐藏属性ScaffoldColumn,使用EditorForModel生效
[ScaffoldColumn(false)]
public string Username { get; set; }
如果设置为false,则该字段不会在View层显示,里面定义的验证也不会生效。
DisplayFormat格式化已短时间形式显示显示
[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString="{0:d}")]
public decimal Time{ get; set; }
以货币格式显示数值
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal Pay{ get; set; }
ReadOnly只读属性,在页面上可以显示但无法将值传入控制器
[ReadOnly(true)]
public decimal Total { get; set; }
DataType数据类型
[Required]
[DataType(DataType.Password)]
[Display(Name="Password")]
public string Password { get; set; }
MVC数据验证Model Validation的更多相关文章
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- MVC 3 数据验证 Model Validation 详解
在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...
- (转)MVC 3 数据验证 Model Validation 详解
继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- MVC 数据验证【转】
[转自]http://www.cnblogs.com/dozer/archive/2010/04/12/MVC-DataAnnotations.html 作者Dozer 今天在这里给大家介绍一下MVC ...
- MVC数据验证
深入浅出 MVC 数据验证 2.0 [附演示源码] 今天在这里给大家介绍一下MVC的数据验证框架. 在1.0版中,很多朋友提出了怎么使用客户端验证,今天找了一些资料,发现了客户端验证的方法. 1.MV ...
- MVC 数据验证
MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...
- MVC 数据验证[转]
前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...
- MVC数据验证使用小结
原文:MVC数据验证使用小结 描述:MVC数据验证使用小结 内容:display,Required,stringLength,Remote,compare,RegularExpression 本人最近 ...
随机推荐
- 你一定喜欢看的 Webpack 2.× 入门实战(转载)
最近在学习 Webpack,网上大多数入门教程都是基于 Webpack 1.x 版本的,我学习 Webpack 的时候是看了 zhangwang 的 <<入门 Webpack,看这篇就够了 ...
- Eclipse 生成WebService客户端代码
1. 打开Eclipse,新建一个普通的Javaproject,然后在新建的项目上右键点击项目,New---->other---->Web Services -------->Web ...
- nmap原理及使用方法
NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工具包. 1简介 nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连接端,并且推断 ...
- Chrome/FireFox处理JSON的插件
Chrome/FireFox处理JSON的插件 JSON插件 效果对比 对于json的数据如果不编排一下格式查看起来很费劲,今天推荐一款chrome/Firfox下处理json的插件JSON-ha ...
- 更改"xxxx" 的权限: 不允许的操作
[摘要:做为root用户,用chmod为何改没有了文件权限 以ROOT用户上岸,当用chmod改文件权限时,体系表现无权变动,为何 文件名是:aa chmod 777 aa chmod: changi ...
- http://www.blogjava.net/crespochen/archive/2011/04/22/348819.html
http://blog.csdn.net/supersky07/article/details/7407523 http://blog.csdn.net/cuker919/article/detail ...
- HTTP状态码及说明
- CocoSourcesCS 1
CocoSourcesCS 1 /*------------------------------------------------------------------------- Compiler ...
- [Algorithom] Shuffle an array
Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...
- minic 动作句型处理
#include "lex_define.h" enum keywords_type//代表一些关键字 { loop_for=,//代表for关键字 loop_while,//代表 ...