Home > OS >  Localization of DataAnnotations errors, can't set ErrorMessageResourceType
Localization of DataAnnotations errors, can't set ErrorMessageResourceType

Time:11-28

I'm trying to localize my model errors from Resources, in .net 6 mvc app.

I set up localization like this:

`builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddMvc()
    .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();`

Then, my folder structure of resources files is like this Resources->Views->{ControlerName}->{ViewName}.resx

In Views, my localization works perfect with IViewLocalizer.

I'm trying to use those resx files for DataAnnotations errors, like this

[Required (ErrorMessageResourceType = (typeof({ProjectName}.Resources.Views.Home.Index)),
            ErrorMessageResourceName = "RequiredField")]

I got error "The type or namespace name 'Resources' does not exist in the namespace '{ProjectName}' (are you missing an assembly reference?)"

What should I put for "typeof"?

CodePudding user response:

If anyone else stuck on this, my problem was location of Resources file.

Which was like this. Resources->Views->{ControlerName}->{ViewName}.resx A path to the View where I will use that model. But instead of this. We need resources file for models also, they can't use Resources file od the view. But we need something like: Resources->Models->DTOs->{ModelName}.resx

  • Related