为 ASP.Net MVC 2 中的非属性模型验证提供本地化错误消息? [英] Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

查看:33
本文介绍了为 ASP.Net MVC 2 中的非属性模型验证提供本地化错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DataAnnotations 属性使用 ASP.Net MVC 2 为我的 ViewModel 提供模型验证:

I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels:

public class ExamplePersonViewModel {
    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))]
    [StringLength(128, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(Resources.Validation))]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))]
    [DataType(DataType.Text)]
    public int Age { get; set; }
}

这似乎按预期工作(尽管它非常冗长).我遇到的问题是正在执行的幕后模型验证与任何特定属性无关.上述模型中的一个例子是 Age 属性需要是一个 int.如果您尝试在表单上输入非整数值,则会出错并显示以下(非本地化)消息:

This seems to work as expected (although it's very verbose). The problem I have is that there are behind-the-scenes model validations being performed that are not tied to any specific attribute. An example of this in the above model is that the Age property needs to be an int. If you try to enter a non-integer value on the form, it will error with the following (non-localized) message:

年龄字段必须是数字.

如何本地化这些非属性验证消息?

是否有这些消息的完整列表,以便我确保它们都已本地化?

推荐答案

转到http://forums.asp.net/p/1512140/3608427.aspx,观看 2010 年 1 月 9 日下午 6:20 的 bradwils 消息.

Go to http://forums.asp.net/p/1512140/3608427.aspx, watch the bradwils message dated 01-09-2010, 6:20 PM.

该解决方案对我很有效.

The solution works well for me.

了解可覆盖消息的完整列表应该很有趣...

It should be interesting to know the complete list of the messages overridable...

更新

这里是帖子内容:

在 App_GlobalResources 中创建一个全局资源类,并将 DefaultModelBinder.ResourceClassKey 设置为该类的名称(例如,如果您制作了Messages.resx",则将 ResourceClassKey 设置为Messages").

Create a global resource class in App_GlobalResources, and set DefaultModelBinder.ResourceClassKey to the name of this class (for example, if you made "Messages.resx", then set ResourceClassKey to "Messages").

您可以在 MVC 2 中覆盖两个字符串:

There are two strings you can override in MVC 2:

  • 当用户输入的数据与数据类型不兼容时(例如,为整数字段键入abc"),使用PropertyValueInvalid"的字符串值.对此的默认消息是:值 '{0}' 对 {1} 无效."
  • 当用户没有为不可为空的字段(例如,整数字段)输入任何数据时,将使用PropertyValueRequired"的字符串值.对此的默认消息是:需要一个值."

在第二种情况下需要注意的是,如果您的验证器提供程序列表中有 DataAnnotationsModelValidatorProvider(默认情况下),那么您将永远不会看到第二条消息.此提供程序会看到非可选字段并向它们添加隐含的 [Required] 属性,以便它们的消息将与具有显式 [Required] 属性的其他字段保持一致,并确保您获得必需字段的客户端验证.

It's important to note in the second case that, if you have the DataAnnotationsModelValidatorProvider in your validator providers list (which it is by default), then you will never see this second message. This provider sees non-optional fields and adds an implied [Required] attribute to them so that their messages will be consistent with other fields with explicit [Required] attributes and to ensure that you get client-side validation for required fields.

这篇关于为 ASP.Net MVC 2 中的非属性模型验证提供本地化错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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