How to change default validation error message in ASP.NET MVC?

不打扰是莪最后的温柔 提交于 2019-11-26 00:59:50

问题


Say I have this property in my model:

[DisplayName(\"test\")]
[Required(ErrorMessage = \"required\")]
public DateTime? SomeDate { get; set; }

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.\".

How do you modify that message? ASP.NET MVC ignored [DataType(DataType.DateTime, ErrorMessage = \'some other message\')]


回答1:


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:

  • 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 change PropertyValueRequired too add it as well.
  • Add the code DefaultModelBinder.ResourceClassKey = "MyNewResource" to your Global.asax startup code.

You're all set.




回答2:


Asp.Net MVC 2 - Changing the PropertyValueRequired string

this is global setting change.




回答3:


This link might be useful: http://weblogs.asp.net/srkirkland/archive/2011/02/15/adding-client-validation-to-dataannotations-datatype-attribute.aspx



来源:https://stackoverflow.com/questions/6214066/how-to-change-default-validation-error-message-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!