DataAnnotations and Resources don't play nicely

前端 未结 1 1203
不知归路
不知归路 2020-12-17 20:08

I\'m using dataannotations in an MVC2 app and am a little discouraged when trying to use RESX file resources for error messages.

I\'ve tried the following but keep g

相关标签:
1条回答
  • 2020-12-17 20:34

    Yes, what you're doing at the end is basically correct. The ErrorMessageResourceName takes what the name implies, the name of a resource, not the resource itself.

    Resources.ErrorMessages.Required points to the actual (localized) error message (resource). The name of the resource is simply "Required", and the type of the resource manager (used for ErrorMessageResourceType) is the class that contains that resource, in this case the Resources.ErrorMessages class.

    So your declaration should look like this:

    [Required(ErrorMessageResourceType = typeof(Resources.ErrorMessages),
        ErrorMessageResourceName = "Required")]
    public string Something { get; set; }
    
    0 讨论(0)
提交回复
热议问题