默认模型绑定的MVC本地化 [英] MVC Localization of Default Model Binder

查看:118
本文介绍了默认模型绑定的MVC本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图弄清楚如何定位由MVC生成的错误信息。让我使用默认的模型绑定作为一个例子,这样我就可以说明这个问题。

I am currently trying to figure out how to localize the error messages generated by MVC. Let me use the default model binder as an example, so I can explain the problem.

假设我有一个表格,在用户进入thier时代。然后,用户进入十中的形式,而是获得的预期误差

Assuming I have a form, where a user enters thier age. The user then enters "ten" in to the form, but instead of getting the expected error of

年龄必须beween 18和25。

"Age must be beween 18 and 25."

消息

的值十是无效的年龄。

显示。

实体的年龄属性定义如下:

The entity's age property is defined below:

    [Range(18, 25, ErrorMessageResourceType = typeof (Errors), 
        ErrorMessageResourceName = "Age", ErrorMessage = "Range_ErrorMessage")]    
    public int Age { get; set; }

一些挖后,我注意到这个错误文本中的 MvcResources.resx <来自 System.Web.Mvc.Resources.DefaultModelBinder_ValueInvalid / code>文件。

After some digging, I notice that this error text comes from the System.Web.Mvc.Resources.DefaultModelBinder_ValueInvalid in the MvcResources.resx file.

现在,如何创建该文件的本地化版本?

Now, how can create localized versions of this file?

作为一种解决方案,例如,我应该下载MVC源码,并添加 MvcResources.en_GB.resx MvcResources.fr_FR.resx MvcResources.es_ES.resx MvcResources.de_DE.resx ,然后编译我自己的版本 MVC.dll

As A solution, for example, should I download MVC source and add MvcResources.en_GB.resx, MvcResources.fr_FR.resx, MvcResources.es_ES.resx and MvcResources.de_DE.resx, and then compile my own version of MVC.dll?

但我不喜欢这个主意。任何其他人知道一个更好的办法?

But I don't like this idea. Any one else know a better way?

推荐答案

请参阅http://forums.asp.net/p/1512140/3608427.aspx,向下滚动到布拉德·威尔逊的接近这个页面的底部应答(星期六,2010年1月9日,3:20 PM)。有上你可以设置本地化一般性错误消息DefaultModelBinder静态属性。

See http://forums.asp.net/p/1512140/3608427.aspx, scroll down to Brad Wilson's reply near the bottom of that page (Sat, Jan 09 2010, 3:20 PM). There are static properties on the DefaultModelBinder that you can set to localize the generic error messages.

一般错误消息是用来代替你的[范围]消息的原因是,[范围]提供的验证的错误消息,但这种特殊情况下,是一个的结合的错误。但绝对没有办法的框架都不能希望字符串十转换为Int32,因此它甚至不能解雇[范围]验证。这是因为在这个论坛的控件中提到的PropertyValueInvalid键。

The reason a generic error message is used instead of your [Range] message is that [Range] provides a validation error message, but this particular case is a binding error. There's absolutely no way the framework can ever hope to convert the string "ten" to an Int32, so it can't even fire the [Range] validator. This is what the "PropertyValueInvalid" key as mentioned in that forum controls.

这篇关于默认模型绑定的MVC本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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