Asp.Net MVC: Some un-localized default error message?

asp.net-mvc, data-annotations, localization, validation

Solution

I think these answer your two questions:

- Why MVC returns me sometimes error message in french, and sometimes in english, in the same form(and I precise, it's the default error message)

One notable difference is that the `[Required]` attribute performs explicit client-side validation, whereas when your field does not contain a valid DateTime, you get server-side validation through a failure by the default model binder to create a DateTime object from the posted form data. It's quite a different mechanism, which I guess explains the different outcome. It would have been nice if the outcome had been consistent, of course.

- How to replace this message genericly, by indicating a text like "La valeur {0} n'est pas une date valide pour le champ {1}"

There are two ways:

- Tell the default model binder what resource string to use for the error message using the `DefaultModelBinder.ResourceClassKey` property. See the answer to this related question for a description of how to achieve this (for MVC 2 but it hasn't changed in MVC 3)

A nicer method (I think) is to do client-side validation. This allows you to provide the error message string you wish from your localized resources. Do it by adding a `DataType` attribute as per below, assuming you created a resource class called `MyLocalizedResources` with a string having key `DateTimeFormatValidationMessage` translated in French as "La valeur {0} n'est pas une date valide pour le champ {1}":

[Required]
[DataType(DataType.Date, ErrorMessageResourceType = typeof(MyLocalizedResources), ErrorMessageResourceName = "DateTimeFormatValidationMessage")]
[LocalizedDisplayName("PublicationDate", NameResourceType = typeof(LocalizationResources.Views.Composer.BaseInfoForm))]
public DateTime PublicationDate { get; set; }

Problem

I've a fully localized website, which is in mostly in french/english/german. For now, all was going fine, but I did notice a problem with some error message of asp.net MVC. I've one property in my model: ``` [Required] [LocalizedDisplayName("PublicationDate", NameResourceType = typeof(LocalizationResources.Views.Composer.BaseInfoForm))] public DateTime PublicationDate { get; set; } ``` LocalizedDisplayname is an extension of the DisplayNameAttribute, which goes in the Resx file to get the correct translation The thread CurrentCulture and the CurrentCultureUI are in fr-FR, so the message should be displayed in french(like it does with my `[Required]` Attribute, display automatically "Le champ Publication est requis". But in the case of a DateTime, if I enter something which isn't a date, the validator just returns me a "`The value 'asdfasdf' is not valid for Publication.`" So: - Why MVC returns me sometimes error message in french, and sometimes in english, in the same form(and I precise, it's the default error message) - How to replace this message genericly, by indicating a text like "La valeur {0} n'est pas une date valide pour le champ {1}" Thank you very much

Original source

Related problems