asp.net mvc 输入/模型验证多语言 [英] asp.net mvc input / model validation multi language

查看:23
本文介绍了asp.net mvc 输入/模型验证多语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 asp.net mvc 很陌生,我知道我正在尝试找出答案进行输入验证的好习惯.

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, where you can add data annotations to properties in the following way:

[Required(ErrorMessage = "Please enter a product name")]
[Column]
public string Name { get; set; }

这很好,但是我们有一个多语言网站(就像大多数网站一样),所以我们不能只用英文显示错误信息.有什么办法可以解决这个问题?我可以根据用户的语言更改此错误消息@runtime 吗?我应该使用 Jquery 客户端验证吗?

This is quite nice, however we have a multi language website (like most websites), so we can't only show the error messages in English. What can be a way to solve this? Can I change this errormessage @runtime, depending on the user's language? Should I use Jquery client side validation?

感谢您的意见.

更新我已经尝试过 Phil Haack 网站上的代码这将使用静态资源解决问题,但是,我们使用来自数据库的资源而不是静态资源.

Update I've tried the code on the website of Phil Haack This will do the trick with static resources however, we use resources that come from a database not static resources.

如果我为数据注释填写以下内容:

If I fill in the following for the dataannotations:

   [MetadataType(typeof(IncidentsMetaData))]
public partial class INCIDENTS
{
    private class IncidentsMetaData
    {
        [Required(ErrorMessageResourceType = typeof(CustomResourceProviders.DBResourceProviderFactory),
            ErrorMessageResourceName="1277")]
        public string SUBJECT { get; set; }
    }
}

然后我收到以下错误:资源类型CustomResourceProviders.DBResourceProviderFactory"没有名为1277"的可访问静态属性.

Then I get the following error: The resource type 'CustomResourceProviders.DBResourceProviderFactory' does not have an accessible static property named '1277'.

当然没有这样的属性,它应该被一个函数访问.知道我能做些什么吗?tnx

Of course there is no such property, it should be accessed by a function. Any idea what I could do about this? tnx

推荐答案

您可以从 RequiredAttribute 并为属性 错误消息.它可以是这样的:

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";
            }
        }            

    }
}

你也继承自 DisplayNameAttribute 并覆盖DisplayName 属性:

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";
        }
    }
}

您还可以创建从 ValidationAttribute 类.

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

这篇关于asp.net mvc 输入/模型验证多语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆