C# ASP.NET MVC 显示属性通过 Resources.resx 提示文件

问题描述

        [display(ResourceType = typeof(Properties.Resources),Prompt = "What should be here?")]
        [required(ErrorMessageResourceType = typeof(Properties.ResourcesDataAnnotations),ErrorMessageResourceName = "IDrequired")]
        [RegularExpression(@"[0-9]{9}",ErrorMessageResourceType = typeof(Properties.ResourcesDataAnnotations),ErrorMessageResourceName = "IDNotValid")]
        public override string ID { get => base.ID; set => base.ID = value; }

如何通过 Resources.resx 文件设置提示

解决方法

像评论中提到的寒鸦一样使用

    [Display(ResourceType = typeof(Properties.Resources),Prompt = "IDPrompt")]
    [Required(ErrorMessageResourceType = typeof(Properties.ResourcesDataAnnotations),ErrorMessageResourceName = "IDRequired")]
    [RegularExpression(@"[0-9]{9}",ErrorMessageResourceType = typeof(Properties.ResourcesDataAnnotations),ErrorMessageResourceName = "IDNotValid")]
    public override string ID { get => base.ID; set => base.ID = value; }

其中 IDPrompt 是 Resources.resx 文件中的静态字符串名称。