1

I have the following properties in my View Model:

public decimal Credit { get; set; }

[EmailAddress]
public string TestEmail { get; set; }

And I got these 2 error messages on Serverside validation, using @Html.ValidationSummary():

  • The value '97,50 €' is not valid for Credit.
  • Das Feld TestEmail enthält keine gültige E-Mail-Adresse.

The second one is perfect translated using the [EmailAddress] Attribute. However the first one, is still in english. Why?

I have googled a bit but every solution is talking about customizing the Validation-messages. What I want is the default Validation-message, but translated to my Culture, like the [EmailAddress] Attribute.

I have this in my web.config:

<system.web>
    <globalization culture="de-DE" uiCulture="de-DE"/>

So how can I make the message The value '97,50 €' is not valid for Credit. be translated to another language?

I have not included the client side validation scripts.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111

1 Answers1

1

Create a Messages.resx in the folder App_GlobalResources. Add the following line:

PropertyValueInvalid | Der Wert "{0}" ist für das Feld "{1}" nicht erlaubt.

Then point to the Messages.resx in you Application_Start()

ClientDataTypeModelValidatorProvider.ResourceClassKey = "Messages";
DefaultModelBinder.ResourceClassKey = "Messages";

Some others which might be helpfull:

FieldMustBeDate | Das Feld "{0}" ist kein gültiges Datum.

FieldMustBeNumeric | Das Feld "{0}" ist keine gültige Zahl.

PropertyValueRequired | Das Feld "{0}" ist erforderlich.

Community
  • 1
  • 1
wertzui
  • 5,148
  • 3
  • 31
  • 51
  • But why does `[EmailAdress]` work? Is .NET realy only partitial localized? – Christian Gollhardt Jan 28 '16 at 16:30
  • 1
    @ChristianGollhardt that just a bug in MVC default localization. You only find one, but there [are others](http://stackoverflow.com/questions/15377131/customize-the-error-message-mvc-for-an-invalid-datetime-in-asp-net-mvc4). – teo van kot Jan 28 '16 at 16:56
  • Strange. Didn't expect that there are bugs in basic things like that in such a battle tested framework. @teovankot – Christian Gollhardt Jan 28 '16 at 17:07
  • 1
    I didn't tried it already, but I think we need to define [`DefaultModelBinder.ResourceClassKey`](http://stackoverflow.com/a/17901575/2441442). If so, I think this should be added to the solution for future readers. – Christian Gollhardt Jan 28 '16 at 17:17