asp.net mvc input / model validation multi language

前端 未结 3 2026
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 08:55

I\'m quite new to asp.net mvc, and right know I\'m trying to find out a good practise to do input validation.

In the project we\'re going to use entity framework, wh

相关标签:
3条回答
  • 2020-12-25 09:31

    Phil Haack has written a good blog post that covers how to do this. Essentially it is much the same except you use resource files to provide the messages.

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Required")]
    public string MyProperty{ get; set; }
    
    0 讨论(0)
  • 2020-12-25 09:35

    You can inherit custom attribute from RequiredAttribute and set your own localized message for property ErrorMessage. It can looks like this:

    public class LocalizedRequiredAttribute : RequiredAttribute
    {
        public LocalizedRequiredAttribute()
            : base()
        {
            // prefix for the selection of localized messages from datebase
            // e.x. for "Required" string, localized messages will be: "RuRequired", "EnRequired"
            var currentCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
    
            // logic to get value from datebase
            // e.x. using Linq2Sql
            using (var context = new dateBaseContext())
            {
                var query = (from x in context.LocalizedStrings
                             where x.NameKey == currentCulture + "Required"
                             select x.NameValue).SingleOrDefault();
    
                if (query != null)
                {
                    base.ErrorMessage = query;
                }
                else
                {
                    base.ErrorMessage = "UndefinedName";
                }
            }            
    
        }
    }
    

    also and you inherit from DisplayNameAttribute and override DisplayName property:

    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        public LocalizedDisplayNameAttribute(string displayNameKey)
            : base(displayNameKey)
        {
        }
    
        public override string DisplayName
        {
            get
            {
                if (!string.IsNullOrEmpty(base.DisplayName))
                {
                    // prefix for the selection of localized messages from datebase
                    // e.x. if DisplayName is "Country", localized messages will be: "RuCountry", "EnCountry"
                    var currentCulture = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
    
                    // logic to get value from datebase
                    // e.x. using Linq2Sql
                    using (var context = new dateBaseContext())
                    {
                        var query = (from x in context.DisplayNames
                                     where x.DisplayNameKey == currentCulture + base.DisplayName
                                     select x.DisplayNameValue).SingleOrDefault();
    
                        if (query != null)
                        {
                            return query;
                        }
    
                        return base.DisplayName;
                    }
                }
    
                return "UndefinedName";
            }
        }
    }
    

    also you can create your custom validation attributes that inherits from ValidationAttribute class.

    0 讨论(0)
  • 2020-12-25 09:41

    Take a look at this post, http://helios.ca/2010/02/17/asp-net-mvc-2-model-validation-with-localization/ good blog on the problem

    0 讨论(0)
提交回复
热议问题