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

推荐订阅源

Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
B
Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
U
Unit 42
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
C
Check Point Blog
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
小众软件
小众软件
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
P
Proofpoint News Feed
罗磊的独立博客
量子位
D
Docker
博客园_首页
D
DataBreaches.Net
Project Zero
Project Zero
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
Security Latest
Security Latest
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
大猫的无限游戏
大猫的无限游戏

博客园 - 无常

用 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

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