2

In order to be able to translate my data annotations in my model with a resource file, I saw that many people recommend the solution offered by jgauffin.

However, when I follow the localization tutorial my project cannot launch.

The problematic code is this one, which is supposed to go in the Global.asax.cs file:

ModelValidatorProviders.Providers.Add(
  new LocalizedModelValidatorProvider(stringProvider)
);

It says that the LocalizedModelValidatorProvider constructor does not take any arguments, which is also shown by other tutorials.

But when I change the line like this:

ModelValidatorProviders.Providers.Add(
  new LocalizedModelValidatorProvider()
);

I get the following error in the browser:

Attempted to access an element as a type incompatible with the array.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.

The griffin.mvccontrib packages were installed with NuGet under Visual Studio 2012. Any idea what I am doing wrong ?

Community
  • 1
  • 1
Andreas Schwarz
  • 1,788
  • 17
  • 42

1 Answers1

0

You might find this link helpful

Here is how I did the registration in the above link:

ResourceStringProvider myResouceFile = new ResourceStringProvider(ModelsResources.ResourceManager);
//ModelsResources is my resource file generated class
GriffinStringsProvider griffinStringsProvider = new GriffinStringsProvider(myResouceFile);
ValidationMessageProviders.Clear();
ValidationMessageProviders.Add(griffinStringsProvider);

ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(myResouceFile);
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());

Make sure you are including the right assemblies

using System.Resources;
using Griffin.MvcContrib.Localization;
using Griffin.MvcContrib.Localization.ValidationMessages;

Also the assembly for your Resource file.

A Khudairy
  • 1,422
  • 1
  • 15
  • 26