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的更多相关文章

  1. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...

  2. MVC 3 数据验证 Model Validation 详解

    在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...

  3. (转)MVC 3 数据验证 Model Validation 详解

    继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...

  4. Hibernate Validation,Spring mvc 数据验证框架注解

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  5. MVC 数据验证【转】

    [转自]http://www.cnblogs.com/dozer/archive/2010/04/12/MVC-DataAnnotations.html 作者Dozer 今天在这里给大家介绍一下MVC ...

  6. MVC数据验证

    深入浅出 MVC 数据验证 2.0 [附演示源码] 今天在这里给大家介绍一下MVC的数据验证框架. 在1.0版中,很多朋友提出了怎么使用客户端验证,今天找了一些资料,发现了客户端验证的方法. 1.MV ...

  7. MVC 数据验证

    MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...

  8. MVC 数据验证[转]

    前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...

  9. MVC数据验证使用小结

    原文:MVC数据验证使用小结 描述:MVC数据验证使用小结 内容:display,Required,stringLength,Remote,compare,RegularExpression 本人最近 ...

随机推荐

  1. UNIX域套接字连接mysql

    用户可以在配置文件中指定套接字文件的路径,如--socket=/data/mysql/mysql.sock [root@localhost ~]# mysql -uroot -p123456 -S / ...

  2. java中interface的完整表述

    我用一个工具:java Decompiler反编译工具查看jar包源码的时候,出现了以下代码: public abstract interface AbsITest{} 在网上搜索了一下: 我对这种情 ...

  3. [转]XML中元素(Element)与节点(Node)的区别

    前言: element是特殊的node 一段纯文本即text-node也是node 但不是element w3c的原话是 A node can be an element node, an attri ...

  4. 一次完整的HTTP请求的大致过程(转)

    说明:这些理论基本都来自网上,所以不一定准确,但一定是比较好理解的,如果要刨根问底,最好的方式就是看书,且要看权威的书. 一次完整的HTTP请求所经历的7个步骤 HTTP通信机制是在一次完整的HTTP ...

  5. 理解SQL Server是如何执行查询的---Joe-T :mvp

    http://www.cnblogs.com/Joe-T/ http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-q ...

  6. 猫、路由器、交换机和PC

    转载:http://duanzw102.blog.163.com/blog/static/161838173201392431722650/ 猫是 modem,是有网络供应商,比如电信公司提供的拨号工 ...

  7. 使用Javascript实现ajax示例

    使用原始的javascript实现ajax <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...

  8. Laravel简⃣单⃣的⃣路⃣由⃣

    在⃣routes.php文⃣件⃣中⃣写⃣如⃣下⃣几⃣个⃣函⃣数⃣: Route::get('/', function () { return view('welcome'); }); // 获⃣取⃣a ...

  9. mybatis批量操作-xml方式

    在实际项目中,我们一般都会用到批量insert.delete.update等操作,由于使用频率还是蛮高的,这里就做个简单的记录,供以后学习和参考. 批量insert 在数据库中,批量插入可以是多条in ...

  10. python升级导致yum命令无法使用的解决办法?

    yum是依赖特定的python版本的,不同的linux系统需要的python版本不同. 查看yum的启动脚本:which is yum 头一行指定使用的python版本,这个必须是系统需要的,而不要使 ...