zoukankan      html  css  js  c++  java
  • MVC 多语言记录1 设置默认的ResourceType

    http://stackoverflow.com/questions/3260748/default-resource-for-data-annotations-in-asp-net-mvc

    Add this class somewhere in your project:

     public class ExternalResourceDataAnnotationsValidator : DataAnnotationsModelValidator<ValidationAttribute>
    {
        /// <summary>
        /// The type of the resource which holds the error messqages
        /// </summary>
        public static Type ResourceType { get; set; }
    
        /// <summary>
        /// Function to get the ErrorMessageResourceName from the Attribute
        /// </summary>
        public static Func<ValidationAttribute, string> ResourceNameFunc 
        {
            get { return _resourceNameFunc; }
            set { _resourceNameFunc = value; }
        }
        private static Func<ValidationAttribute, string> _resourceNameFunc = attr => attr.GetType().Name;
    
        public ExternalResourceDataAnnotationsValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
            : base(metadata, context, attribute)
        {
            if (Attribute.ErrorMessageResourceType == null)
            {
                this.Attribute.ErrorMessageResourceType = ResourceType;
            }
    
            if (Attribute.ErrorMessageResourceName == null)
            {
                this.Attribute.ErrorMessageResourceName = ResourceNameFunc(this.Attribute);
            }
        }
    }
    

     and in your global.asax, add the following:

    // Add once
    ExternalResourceDataAnnotationsValidator.ResourceType = typeof(CustomDataAnnotationsResources);
    
    // Add one line for every attribute you want their ErrorMessageResourceType replaced.
    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RangeAttribute), typeof(ExternalResourceDataAnnotationsValidator));
    

    It will look for a property with the same name as the validator type for the error message. You can change that via the ResourceNameFunc property.

    EDIT: AFAIK this works from MVC2 onwards, as DataAnnotationsModelValidatorProvider was introduced in MVC2.


    To achieve this, I created a new class that inherits from RequiredAttribute, and the error message is embedded inside this new class:

    The error message is taken from the ValidationResource.resx file, where I list the error message as follows:

    public class RequiredWithMessageAttribute : RequiredAttribute
    {
        public RequiredWithMessageAttribute()
        {
            ErrorMessageResourceType = typeof(ValidationResource);
            ErrorMessageResourceName = "RequiredErrorMessage";
        }
    }
    

    RequiredErrorMessage --> "{0} is required."

    where {0} = display name.

    I then annotate my models like this, so I never have to repeat my error message declarations:

    [RequiredWithMessage]
    public string Name { get; set; }
    

    Once you do this, an error message ("Name is required.") will appear whenever validation fails.

    This works properly with ASP.NET MVC's server-side validation and client-side validation.

     

  • 相关阅读:
    bzoj2669 [cqoi2012]局部极小值 状压DP+容斥
    bzoj2560 串珠子 状压DP
    bzoj2004 [Hnoi2010]Bus 公交线路 矩阵快速幂+状压DP
    「校内训练 2019-04-23」越野赛车问题 动态dp+树的直径
    bzoj5210 最大连通子块和 动态 DP + 堆
    动态 DP 学习笔记
    bzoj4987 Tree 树上背包
    bzoj1190 [HNOI2007]梦幻岛宝珠 背包
    bzoj1004 [HNOI2008]Cards Burnside 引理+背包
    bzoj4922 [Lydsy1706月赛]Karp-de-Chant Number 贪心+背包
  • 原文地址:https://www.cnblogs.com/ddeef/p/3459623.html
Copyright © 2011-2022 走看看