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

查看:458
本文介绍了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; }

这是相当不错的,但是我们有一个多语言网站(最喜欢的网站)
所以我们不能只显示英文的错误消息。
有什么可以解决此问题的方法?我可以改变这个errormessage的@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?

感谢您的输入。

更新我试过code菲尔哈克的网站上
这将做静态资源的伎俩但是,我们使用来自数据库不是静态资源的资源。

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.

如果我填了dataannotations以下内容:

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

推荐答案

您可以继承<自定义属性href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx\">RequiredAttribute并设置属性自己的本地化消息<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessage.aspx\">ErrorMessage.它可以是这样的:

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

    }
}

也和你从<一个继承href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.aspx\">DisplayNameAttribute并重写<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.displayname.aspx\">DisplayName属性:

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

你也可以从<一个继承创建自定义的验证属性href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx\">ValidationAttribute类。

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

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