博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC数据验证Model Validation
阅读量:4327 次
发布时间:2019-06-06

本文共 4698 字,大约阅读时间需要 15 分钟。

Required必须项验证属性

[Required]public string FirstName { get; set; }        //ID编号       [ScaffoldColumn(false)]       [Required(AllowEmptyStrings = false, ErrorMessage = "用户ID不能为空")]       [Display(Name = "记录编号", Order = 20000)]       public int ID { get; set; }

StringLength长度

[Required][StringLength(160)]public string LastName { get; set; }[Required][StringLength(160, MinimumLength=3)]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(35,44)]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(160, 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(160, MinimumLength=3)][Display(Name="First Name")]public string FirstName { get; set; }
[Display(Name = "身份证号码")]        [RegularExpression(@"\d{17}[\d|x]|\d{15}", ErrorMessage = "身份证号码格式错误")]                
@Html.LabelFor(t => t.IdentityNo)
@Html.EditorFor(model => model.IdentityNo) @Html.ValidationMessageFor(model => model.IdentityNo)

自动生成编辑页面

Shipping Information@Html.EditorForModel()

  

@using (Html.BeginForm()) { @Html.ValidationSummary(true)
UserInfo
@Html.LabelFor(t => t.UserPassword)
@Html.EditorFor(model => model.UserPassword) @Html.ValidationMessageFor(model => model.UserPassword)
@Html.LabelFor(t => t.IdentityNo)
@Html.EditorFor(model => model.IdentityNo) @Html.ValidationMessageFor(model => model.IdentityNo)
@Html.LabelFor(t => t.Email)
@Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email)
@Html.LabelFor(t => t.Age)
@Html.EditorFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age)
@Html.LabelFor(t => t.Money)
@Html.EditorFor(model => model.Money) @Html.ValidationMessageFor(model => model.Money)
@Html.LabelFor(t => t.TEmail)
@Html.EditorFor(model => model.TEmail) @Html.ValidationMessageFor(model => model.TEmail)
@Html.EditorForModel()
}

隐藏属性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; }

 

 

转载于:https://www.cnblogs.com/shy1766IT/p/5080972.html

你可能感兴趣的文章
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>