How to change default validation error message in ASP.NET MVC?
69,095
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 changePropertyValueRequired
too add it as well. - Add the code
DefaultModelBinder.ResourceClassKey = "MyNewResource"
to your Global.asax startup code.
You're all set.
Author by
Sedat Kapanoglu
author of street coder · ex-microsoft engineer in microsoft windows cosd · founder of eksi sozluk · demoscene old-timer github · twitter · linkedin · pouet
Updated on February 24, 2020Comments
-
Sedat Kapanoglu about 4 years
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')]
-
rafek about 12 yearsHelpful, thx. Seems that the issue isn't solved even in MVC3 which is sad. So what does ErrorMessage for DataType attribute do?
-
kbvishnu over 11 yearsIts too heavy that we need to do add resx file for this. :(
-
Sedat Kapanoglu over 10 yearsWe have switched to MVC3 and then MVC4, no problems so far.
-
mahdi gh over 10 yearsi created a new mvc 3 project, and did all of the steps. even i changed the resource file to embedded resource, but still shows "The User name field is required" for the default AccountController. please tell me what to do? or if you can make mvc3 sample.I'm sure there are a lot of people who have this problem.thanks
-
Sedat Kapanoglu over 10 years@mahdigh you can ask it as a SO question so everyone can chip in troubleshooting it?
-
mahdi gh over 10 yearshere is the link for my question.please take a look.thanks stackoverflow.com/questions/18330420/…