Using Validation Annottions 使用验证

简介: You can use four attributes in the DataAnnotations namespace to cover common validation scenarios. Required    特性 Like all the built-in validation a...

You can use four attributes in the DataAnnotations namespace to cover common validation scenarios.

Required    特性

Like all the built-in validation attributes,the Required attribute delivers both server-side and client-side  validation logic(alothough internally,it is another component in the MVC framework that delivers the client-side validation logic for the attribute through a validation adapter design).

Required 特性是服务器端和客户端同时存在的,即使用户禁用了javascript,但是服务器的验证也是会起作用的。

2.StringLength  字符串长度

MinimumLength表示的是字符串的最小长度

3.RegularExpression 正则表达式 

4.Range 

The Range attribute specifies minimum and maximum constraints for a numerical value.The first parameter to the attribute is the minimum value,and the second parameter is the maximum value.The values are incluse(包含).就是说Range特性是包含那两个参数值的。

上面的四个验证来自System.ComponentModel.DataAnnotations  命名空间

下面介绍两个来自System.Web.Mvc命名空间中的验证特性

Remote特性  可以用来进行客户端与服务器的动态交互

服务器端会返回包含true或false的json数据

The second attribute is Compare.  这时用来比较一个输入的值是否和另外一个输入的值相同的特性。

对于每一个验证出错都会有对应的 出错信息,这个信息在所有的验证中都是可以自己定制的。并且可以进行错误信息的本地化操作。

 

The validation features of Asp.net MVC are part of a coordinate system involving model binders,model metadata,model validators  and model state.

By default,the asp.net MVC framework executes validation logic during model binding.MVC框架执行数据验证是在Model Binding 即 数据绑定的时候。或者说是模型绑定的时候。

The model binder catches all the failed validation rules and places them into model state.

Model  State  模型状态 是个值得研究的东西,因为他包含了错误信息,以及出现错误的值

ModelState.IsValid 表示当前有效

可以自己定义需要的验证特性,只需要让他继承自ValidationAttribute特性即可在任何你想使用的地方使用该特性进行验证。

Display Attribute  

The Display attribute sets the friendly “display name”   for a model property.You can use the Display attribute to fix the label for the FirstName field.

ScaffordColumn  

The ScaffoldColumn attribute hides a property from HTML helpers such as EditorForModel and DisplayForModel.

DisplayFormat  表示的是对数值的格式化操作  要使该特性有效,必须使ApplyFormatInEditModel有效

DataType 表示的数值的类型 例如:[DataType(DataType.Password)]  表示一个密码

UIHint attribute

The UIHint attribute gives the asp.net MVC runtime the name of template to use when rendering output with the template helpers(like DisplayFor  and EditorFor).You can define your own template helpers to override the default MVC behavior.

HiddenInput  attribute

The HiddenInput attribute lives in the System.Web.Mvc namespace and tells the runtime to render an input element with a type of ‘hidden’.

以上这些就是在Model验证中的特性类,以后我会逐个的分析每个特性的用处。

 

 

我又回来了,回到了技术最前线,
相关文章
|
XML Java 程序员
Spring参数注入的几种方法
Spring参数注入的几种方法
257 0
|
前端开发 Android开发
使用Android Studio(AS)查看apk信息
使用Android Studio(AS)查看apk信息
1005 0
使用Android Studio(AS)查看apk信息
|
存储 SQL 缓存
StarRocks常见面试问题(一)
StarRocks常见面试问题(一)
|
边缘计算 运维 安全
出海浪头之上,共探CDN进化新支力
阿里云CDN技术沙龙·深圳
266 1
|
存储 缓存 监控
【嵌入式SD NAND】基于FATFS/Littlefs文件系统的日志框架实现
综上所述,构建一个基于FATFS/Littlefs文件系统的日志框架需要对文件系统的操作有深入理解,并以此为基础设计一套完整的日志处理机制。这样的框架不仅能够确保日志数据的完整性和系统的鲁棒性,同时还能够满足嵌入式系统对于性能和资源使用的严格要求。
600 4
|
机器学习/深度学习 PyTorch 算法框架/工具
【机器学习】Softmax回归探索
【机器学习】Softmax回归探索
500 8
|
12月前
|
人工智能 API Apache
推荐3款开源、美观且免费的WinForm UI控件库
推荐3款开源、美观且免费的WinForm UI控件库
2090 6
|
安全 Linux Python
【安全狗】linux免费服务器防护软件安全狗详细安装教程
【安全狗】linux免费服务器防护软件安全狗详细安装教程
1275 0
|
消息中间件 存储 运维
RocketMQ与Kafka深度对比:特性与适用场景解析
RocketMQ与Kafka深度对比:特性与适用场景解析
|
PyTorch 算法框架/工具 C++
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..