How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

后端 未结 1 665
[愿得一人]
[愿得一人] 2020-12-01 07:52

I\'m building a MVC web application with C#. Since the site will be multilingual, I\'ve implemented my own ResourceManager. This class is responsible for fetching the requir

相关标签:
1条回答
  • 2020-12-01 08:35

    The RequiredAttribute allows to use a custom resource manager:

    [Required(
        ErrorMessageResourceType = typeof(CustomResourceManager), 
        ErrorMessageResourceName = "ResourceKey")]
    public string Username { get; set; }
    

    UPDATE:

    Another possibility is to write your custom attribute:

    public class CustomRequiredAttribute : RequiredAttribute
    {
        public override string FormatErrorMessage(string name)
        {
            return YourCustomResourceManager.GetResource(name);
        }
    }
    
    0 讨论(0)
提交回复
热议问题