如何在ASP.NET MVC改变默认的验证错误消息? [英] How to change default validation error message in ASP.NET MVC?
问题描述
说我有在我的模型此属性:
Say I have this property in my model:
[DisplayName("test")]
[Required(ErrorMessage = "required")]
public DateTime? SomeDate { get; set; }
当您在 Html.TextBoxFor(型号=> model.SomeDate)输入ASDF
,你会得到验证错误消息值ASDF是不适用于测试。
when you type in "asdf" in Html.TextBoxFor(model => model.SomeDate)
, you get the validation error message "The value 'asdf' is not valid for test.".
你如何修改该消息? ASP.NET MVC忽略 [数据类型(DataType.DateTime,的ErrorMessage ='一些其他的消息')
How do you modify that message? ASP.NET MVC ignored [DataType(DataType.DateTime, ErrorMessage = 'some other message')]
推荐答案
显然,我的问题已经回答了在<一个href=\"http://stackoverflow.com/questions/1538873/how-to-replace-the-default-modelstate-error-message-in-asp-net-mvc-2\">How在Asp.net替换默认的ModelState错误信息MVC 2?。
Apparently my question was already answered at How to replace the default ModelState error message in Asp.net MVC 2? .
我会在这里总结一下吧:
I'll summarize it here:
- 创建为您的项目文件夹App_GlobalResources文件(右键单击项目 - >添加 - >添加文件夹ASP.NET - > App_GlobalResources文件)。
- 添加RESX文件文件夹中。说
MyNewResource.resx
。 - 添加资源键
PropertyValueInvalid
与所需的消息格式(例如,内容{0}是现场无效{1})。如果你想改变PropertyValueRequired
太添加它。 - 添加code
DefaultModelBinder.ResourceClassKey =MyNewResource
您的Global.asax启动code。
- Create App_GlobalResources folder for your project (right click to project -> Add -> Add ASP.NET folder -> App_GlobalResources).
- Add a resx file in that folder. Say
MyNewResource.resx
. - Add resource key
PropertyValueInvalid
with the desired message format (e.g. "content {0} is invalid for field {1}"). If you want to changePropertyValueRequired
too add it as well. - Add the code
DefaultModelBinder.ResourceClassKey = "MyNewResource"
to your Global.asax startup code.
您准备就绪。
这篇关于如何在ASP.NET MVC改变默认的验证错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!