views:

731

answers:

5

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.

+1  A: 

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.

tvanfosson
A: 

I could call ModelState.Clear(); at the start of the action. But, I'm not sure I like having to do that in each action.

Vyrotek
A: 

You could make the DateTime parameter nullable. Then if nothing is supplied the error will not be added to ModelState.

public ActionResult Method(Datetime? date)
{}

rather than

public ActionResult Method(Datetime date)
{}
Schotime
+5  A: 

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.

Craig Stuntz
A: 

Hi having same problem the errors are being added by the framework and I'm using EntLib VAB. The issue is thate the properties are nullable and if the vallue "abc" is added into an int the message is automatically added to the modelstate and therefore shows up in validationsummary. I'm on MVC 2 RC2 and am wondering how to go about disabling the default behavior and adding my own messages and not the default ones. Like I said making it nullable does not seem to work.

khan