惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
Hacker News: Ask HN
Hacker News: Ask HN
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
WordPress大学
WordPress大学
U
Unit 42
PCI Perspectives
PCI Perspectives
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
I
Intezer
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
Martin Fowler
Martin Fowler
B
Blog
美团技术团队
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
博客园_首页
A
About on SuperTechFans
Vercel News
Vercel News
Attack and Defense Labs
Attack and Defense Labs
H
Heimdal Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
小众软件
小众软件
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
Y
Y Combinator Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
T
Tenable Blog

博客园 - 无常

用 Zig 写了个 MCP Server,让 AI Agent 直接操控你的 Outlook ABP点滴:API无权访问资源时,返回 PolicyName 信息 使用MSBUILD 构建时出错 error MSB3086: Task could not find "sgen.exe" using the SdkToolsPath PPTPD默认MTU太大引起一些网站上不了的问题 CentOS 6.0 安装配置rails 2.3.11 + redmine 1.2.1 笔记 MVC3中实现验证提示信息多语言支持 nginx 截断日志一个批处理 在没有安装有mvc3的主机上部署asp.net mvc3网站,需要包含的DLL文件 ASP.NET MVC 2 RTM client side validation一个隐秘的坑 Python:使用ctypes库调用外部DLL NHibernate+Oracle 遇到ORA-01461和ORA-01084及解决方法 ASP.NET MVC中实现多个按钮提交的几种方法 - 无常 - 博客园 为cnblogs定做一个代码插入代码的windows live writer插件 MVC 2.0: ConvertEmptyStringToNull 带来烦恼 Code: jsTree ajax 选择行政区域 送出15个Google Wave邀请,需要的赶快 GeekOS:Project1. Loading Executable Files GeekOS:二、Project0 GeekOS: 一、构建基于Ubuntu9.04的实验环境 动刀EFOracleProvider,使其支持char、timestamp(x)等类型
MVC3中使用验证适配器修改默认的验证提示信息
无常 · 2011-05-29 · via 博客园 - 无常

2011-05-29 14:36  无常  阅读(5841)  评论()    收藏  举报

验证

MVC中提供的模块验证非常灵活,在System.ComponentModel.DataAnnotations命名空间中提供了常用的一些验证特性,如Required、Compare等。 所有的验证特性都提供有ErrorMessage属性给我们设置自定义的出错提示信息,比如我们可以这样指定必填属性的提示:[Required(ErrorMessage ="请输入用户名")]。 还可以通过设置ErrorMessageResourceType和 ErrorMessageResourceName属性来使用系统资源文件实现多语言支持(参考:http://haacked.com/archive/2009/12/12/localizing-aspnetmvc-validation.aspx)。

问题

项目中遇到问题,程序员为了省事验证属性都最简化,比如必填字段只指定了[Required]特性,这样就导致了验证错误提示信息不太直观,比如会这样:

image  image

客户比较挑剔,特别指定要消除鸡肠文。按照上文提到的方法,可以给Required.Errormessage指定详细的验证信息,但项目中的实体类较多,一一去修改的话工作量比较大。
于是寻找捷径…

验证适配器

DataAnnotationsModelValidatorProvider提供有RegisterAdapter和RegisterAdapterFactory二个方法供我们注册自己的验证适配器或适配器工厂类。首先创建一个自己的适配器MyRequiredAttributeAdapter:

image

从MVC内置的System.Web.Mvc.RequiredAttributeAdapter适配器类继承(RequiredAttributeAdapter的基类y就是DataAnnotationsModelValidator<RequiredAttribute>),覆盖GetClientValidationRules方法,在这里使用自定义的提示文字创建验证规则类。

然后在程序启动时注册此适配器:

image

编译后再打开页面,提示信息已经全部改变了。
image

省去了一个一个修改的麻烦。