微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 在ASP.Net MVC 2中为非归因模型验证提供本地化错误消息?

我正在使用 DataAnnotations属性以及ASP.Net MVC 2为我的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。如果您尝试在表单上输入一个非整数值,则会出现以下(非本地化)消息的错误

The field Age must be a number.

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

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

解决方法

转到 http://forums.asp.net/p/1512140/3608427.aspx,观看bradwils消息日期:01-09-2010,6:20 PM。

这个解决方案对我来说很好。

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

UPDATE

这里的帖子内容

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”).

There are two strings you can override in MVC 2:

  • The string value for “PropertyValueInvalid” is used when the data the user entered isn’t compatible with the data type (for example,typing in “abc” for an integer field). The default message for this is: “The value ‘{0}’ is not valid for {1}.”
  • The string value for “PropertyValuerequired” is used when the user did not enter any data for a field which is not nullable (for example,an integer field). The default message for this is: “A value is 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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: ' Style="position: relative" AppendDataBoundItems="True"> (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke '*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you