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验证中的特性类,以后我会逐个的分析每个特性的用处。

 

 

我又回来了,回到了技术最前线,
相关文章
|
云安全 安全 网络安全
|
7天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
2002 1
|
14天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
3037 0
|
13天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
2228 44
|
10天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1402 3
|
7天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
7天前
|
开发者 索引
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用
Open Plugin Market 是一个开放插件索引平台,收录800+优质DSH插件,支持分类浏览、智能推荐与随机发现。无需安装市场,粘贴索引地址即可一键接入,网页版直访 dshopen.org,助力小开发者被看见。(239字)
1634 1
DeepSeekHarness大肥鱼自带插件市场了?加个地址就能用
|
6天前
|
人工智能 Linux API
Magpie, 轻量本地AI网关!免费Token用不完!
Magpie 是开源AI编程模型调度网关,上线一月获1.8万Star。它通过本地高性能网关统一托管多平台API密钥、账号与免费额度,实现跨Agent的模型秒级切换、静默额度轮换与资源复用,显著降低碎片化管理成本。(239字)
Magpie, 轻量本地AI网关!免费Token用不完!
|
6天前
|
安全 程序员 应用服务中间件
PHP 被曝两大致命漏洞,请所有程序员抓紧做这件事!
PHP 官方发布了紧急安全更新,一口气修复了两个严重漏洞。如果网站和服务器用了 PHP 应该怎么处理?漏洞的原理是什么?本文给你讲清楚
687 0

热门文章

最新文章