Vyrotek
Vyrotek

Reputation: 5459

How do you turn off or replace the default ModelState error message in Asp.net MVC?

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.

Upvotes: 9

Views: 2405

Answers (4)

Craig Stuntz
Craig Stuntz

Reputation: 126547

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.

Upvotes: 8

Schotime
Schotime

Reputation: 15987

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)
{}

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532465

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.

Upvotes: 1

Vyrotek
Vyrotek

Reputation: 5459

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.

Upvotes: 0

Related Questions