更改默认的错误消息在MVC3数据类型 [英] Change the default error message for data type in mvc3
问题描述
我在MVC 3开发和有一个小问题。
我想改变默认的错误消息无效的数据类型。
让说我的道具价格的模式,我想输入AAA将是他的错误消息,你可以在这里输入的唯一价值是多少。
I'm developing in mvc 3 and have a little question. I want to change the default error message for invalid data type. let say I've a model with the prop Price, and I want his error message for input "aaa" will be "The only value you can enter here is a number".
什么是这样做的最简单的方法?
(我想这样做我所有车型)
what is the easiest way of doing that? (I want to do it for all of my models)
推荐答案
您可以在您的模型属性,例如使用常规的前pression数据注释:
You could use a Regular Expression data annotation on your model property, e.g.:
[RegularExpression(@"^[0-9\.]*$", ErrorMessage="The only value you can enter here is a number")]
public double Price { get; set; }
您应该从一个白名单点接近验证 - 即什么应该被允许通过,而不是一个黑名单,这将是什么是无效的。
You should approach validation from a white list point of view - i.e. what should be allowed through, as opposed to a black list, which would be what is invalid.
点击此处了解详情:
http://www.asp.net/mvc/教程/ MVC音乐商店部分-6
希望这有助于!
山姆
这篇关于更改默认的错误消息在MVC3数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!