Search code examples
asp.net-mvcglobalization

Access to Resources resx file failing. (MVC & ErrorMessageResourceType)


Been troubling me for hours this! As a newbie to MVC, have followed various ways of trying to access a resource file in the App_GlobalRecources folder.

The RESX file properties are Embedded Resource as build action, and custom tool is set to PublicResXFileCodeGenerator.

I also have this in the web.config (system.web):

<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />

In a view model, (although have tried accessing from functions with the same problem), I have the following:

Public Class ExternalLoginConfirmationViewModel
    <Required(ErrorMessageResourceType:=TypeOf (Resource1), ErrorMessageResourceName:="Test")>
    <Display(Name:="Email")>
    Public Property Email As String
End Class

The problem is that I get the following error:

'Resource1' is a class type and cannot be used as an expression.

I cannot figure out why as have tried numerous suggestions from numerous websites, and get the same error.


Solution

  • OK - so seemed to have solved it.

    Every website I have found advises the VB below (or the C# equivalent):

    <Required(ErrorMessageResourceType:=TypeOf (Resource1), ErrorMessageResourceName:="Test")>
    

    Not one website has suggested this which seems to have solved the problem, although I don't know why:

    <Required(ErrorMessageResourceType:=GetType(Resource1), ErrorMessageResourceName:="Test")>
    

    Anyway, hope this helps if someone else has the same issue.