[Required(ErrorMessage = "Message")]
[Display(Name="Username")]
public string Name{get;set;}如何让Message和Username可以是引用资源中的字段?

解决方案 »

  1.   

    那你需要自己写Filter类,以及使用自己的Attribute了。
      

  2.   

    验证信息可以直接使用以下方式:
    [Required(ErrorMessageResourceName = "RequiredField", ErrorMessageResourceType = typeof(Resources.Admin))]
    public string Name{get;set;}
    但必须在资源文件里添加RequiredField这个资源key,至于display,我在此可提供一个filter:
    /// <summary>
        /// Localized display attribute name, get the value from resources file
        /// </summary>
        public class LocalizedDisplayName : DisplayNameAttribute
        {
            private string _defaultName = "";        public Type ResourceType { get; set; }        public string ResourceName { get; set; }        public LocalizedDisplayName(string defaultName) { _defaultName = defaultName; }        public override string DisplayName
            {
                get
                {
                    PropertyInfo p = ResourceType.GetProperty(ResourceName);                if (p != null)
                    {
                        return p.GetValue(null, null).ToString();
                    }
                    else
                    {
                        return _defaultName;
                    }
                }
            }
        }
    在实体类调用:
    [LocalizedDisplayName("Name", ResourceName = "Name", ResourceType = typeof(Resources.Admin))]
    public string Name{get;set;}
    如果资源文件没有此KEY,就默认使用"Name"来显示了
      

  3.   


    [LocalizedDisplayName("Name", ResourceName = "Name", ResourceType = typeof(Resources.Admin))]
    他好像访问不到App_GlobalResources里面的字段始终是默认值
      

  4.   

    没理由啊,应该是可以的,我现在都这样使用的。你是读取不了其他语言还是说资源文件里的值都读取不了?再不行的话你就设一个断点调试一下,看是否哪里写错了,代码运行时有没进入到此filter中
      

  5.   

    进去了 但是它找不到资源中的KEY 我记得之前我用的 说是App_GlobalResources不公开什么的。 
    是不是资源文件还有命名规范的原因,但我这个资源文件是默认的 按说不用加什么语言标识吧
      

  6.   

     PropertyInfo p = ResourceType.GetProperty(ResourceName);
    断点执行 这里貌似也取道了ResourceName的值,但是p一直为Null