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 本人最近 ...
随机推荐
- [转]MySQL 数据类型(二)
MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许我们指定数值字段 ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Java如何判断线程池所有任务是否执行完毕
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Tes ...
- virtualbox4.2.18 ubuntu12.04 The system is running in low-graphics mode
参考1:http://askubuntu.com/questions/225090/the-system-is-running-in-low-graphics-mode-error-in-virtua ...
- 如何理解redo和undo
redo和undo的区别 redo--> undo-->datafileinsert一条记录时, 表跟undo的信息都会放进 redo 中, 在commit 或之前, redo 的信 ...
- 通过HTTP发包工具了解HTTP协议
一.HTTP.pl功能简介 HTTP.pl perl编写的发包工具,简化版本curl,像curl致敬(唉,“致敬”都被于妈玩坏了). 该发包工具支持HEAD,GET,METHOD三种基本请求方法, ...
- request.startAsync()不支持异步操作
Servlet3.0使用异步处理时,后台报错: java.lang.IllegalStateException: A filter or servlet of the current chain do ...
- 新型数据库Kudu应用经验分享
小米使用kudu的案例 http://www.aiweibang.com/yuedu/60603532.html 调研kudu的情况
- MongoDB的连接字符串
本文导读:MongoDB数据库与传统的关系型数据库相比,它具有操作简单.完全免费.源码公开等特点,这使MongoDB产品广泛应用于各种大型门户网站和专业网站.由于MongoDB连接并不支持HTTP协议 ...
- 使用JTextArea示例
相对于JLabel显示提示文字,JTextArea有一个先天优势:文字可以拷贝出来.经过下面设置它也能在外观上和JLabel一致. 代码如下: JTextArea txtArea=new JTextA ...