I have a controller action which has a nullable DateTime as one of the parameters which comes from a textbox on my form. I noticed that if the user were to type in "blah" in that textbox then I will get a Null value back for the DateTime param and a model error is automatically added. In this case the ModelState error that is added is "The value 'blah' is not valid".
My problem is that my site supports multiple languages and so I need to localize this error. Normally I just validate and add the ModelState errors myself but in this case I can't seem to get rid of it. If I add another ModelState error for the same textbox it does not show up.
AddModelError() method to add a validationerror to the ModelState. If you then check the IsValid property in the ModelState variable, it will say its not valid. If you call ModelState. Clear() and then check the IsValid property again it will be valid.
You can use SelectMany function c# to get error message from modelstate mvc. It will generate error message string it contains modelstate errors; we can return as json and display in html element. You have to define validations inside the class as per requirement.
In short, the ModelState is a collection of name and value pairs that are submitted to the server during a POST. It also contains error messages about each name-value pair, if any are found. ModelState is a property of a Controller instance, and can be accessed from any class that inherits from Microsoft.
I have to strongly disagree with all of the answers you have received so far. None of them actually answer the question you asked, which is how to localize the MVC error messages. You could spend a lot of effort working around this single instance of the problem, and still have the same problem with the 50 other cases of MVC error messages if you don't actually localize your application. They are defined in MvcResources.resx, which is found in the Resources folder of the MVC source code (or just browse the assembly with Reflector). In particular, you are looking for the Common_ValueNotValidForProperty
resource. You would localize these messages by providing a localized satellite assembly, in the same way that you localize any .NET application. The specifics of internationalization/localization in .NET applications are outside of the scope of this answer, but there are many books on the subject. Doing this in MVC is no different than doing it in any other application, except that, in most cases, localizations of the framework are already available. MVC is still in beta, so it is, as far as I know, English-only, at the moment. Presumably, that will change after release.
I would remove that particular textbox from the whitelist you pass to TryUpdateModel/Update model and validate it "by hand," rather than having it get validated by the framework. Alternatively, you could try iterating through the ModelState Error collection, find the one you want to remove, and delete it -- say using RemoveItem once you figure out which one you want to get rid of.
EDIT: If you are using the default ModelBinder, you could implement your own. I was assuming that since you were generating your own model errors, you were using TryUpdateModel/UpdateModel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With