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

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

问题描述

我使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx\">DataAnnotations与ASP.Net MVC 2的属性一起为我的ViewModels提供模型验证:

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

这似乎按预期方式工作(尽管这是非常详细)。我的问题是,有背后的幕后正在执行模型验证那些不依赖于任何特定的属性。这与上述模式的一个例子是,年龄属性需要有一个 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:

现场年龄必须是一个数字。

The field Age must be a number.

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

是否有可用的这些消息的完整列表,这样我就可以确保他们所有本地化?

推荐答案

到<一个href=\"http://forums.asp.net/p/1512140/3608427.aspx\">http://forums.asp.net/p/1512140/3608427.aspx,观看日期01-09-2010的bradwils消息,6:20 PM。

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

该解决方案很适合我。

应该是有趣的,知道消息的完整列表可覆盖...

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

更新

下面的内容后:

创建一个App_GlobalResources文件全球资源类,并DefaultModelBinder.ResourceClassKey设置为此类的名称(例如,如果您做了Messages.resx,然后设置为ResourceClassKey消息)。

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(这是默认设置),那么你永远不会看到这第二个消息第二种情况下要注意这一点很重要。该供应商认为非可选字段,并增加了一个隐含的[必需]属性给他们,让他们的消息将带有明确的[必填]等领域一贯的属性,并确保您获得所需的字段客户端验证。

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天全站免登陆