0
votes

I have a solution with two projects. One is called Resources and is a collection of resource .resx files for localization. The other is an MVC 5 project that has Resources as a reference. I define a new resource in the .resx file called EmailAddress and sure enough it is in Resource.Designer,cs:

 internal static string EmailAddress {
            get {
                return ResourceManager.GetString("EmailAddress", resourceCulture);
            }
        }

However, when I try to refernce in my Model class it says it doesn't exist:

[Display(Name = "EmailAddress", ResourceType = typeof(Resources.Resources))]
        [Required(ErrorMessageResourceType = typeof(Resources.Resources),
                  ErrorMessageResourceName = "EmailRequired")]
        [RegularExpression(".+@.+\\..+", ErrorMessageResourceType = typeof(Resources.Resources),
                                         ErrorMessageResourceName = "EmailInvalid")]
        public string EmailAddress { get; set; }

Also, if I try to do this a Razor View, it doesn't show up in the Intellisense:

@Resources.Resources. 

What is going on? I should also state that the resource file was originally taken from a sample project and the resources that were in it show up but any I add don't (but its the designer cs file).

1
Make sure your project has a reference to your resources project. Also in your view you will need to add the @using statement followed by your projectnamespace - heymega
I have both and it still does not find any that I add. The "original set" is there. I get this error on running with the model the way shown above "Cannot retrieve property 'Name' because localization failed. Type 'Resources.Resources' is not public or does not contain a public static string property with the name 'EmailAddress'." - user2471435
What is the Build Action (under file properties when you select the .resx file) of your resources vs the files from the original sample? - Mike Guthrie
Also, when editing your .resx file, is the Access Modifier set to Public or Internal? - Mike Guthrie
They are all set to Internal. I don't see how to change this. The ones that work all internal too. - user2471435

1 Answers

0
votes

I had a very similar problem, in my case it was solved by doing 2 things to those resource files:

  • Set the custom Tool to PublicResXFileCodeGenerator.
  • Set the build action to "Embedded Resource".
  • Additionally added the reference in the web.config for the views to the resources in the namespaces section.

After that, it was solved so I was able to call it as "@Resources.Resources.EmailAddress"