本地化数据注释缺省消息([必需] [StringLength]等) [英] localize data annotations default messages ([Required] [StringLength] etc.)

查看:138
本文介绍了本地化数据注释缺省消息([必需] [StringLength]等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我装饰我的ViewModels的特性,像这样的属性:

if I decorate the properties of my ViewModels with attributes like this:

public class Vm
{

[Required]
[StringLength(35)]
public string Name {get;set;}

}

我要拿到英语验证消息:

I am going to get english validation messages:

"this field is required"
"The field Name must be a string with a maximum length of 35"

我怎么能翻译它们?

how could I translate them ?

推荐答案

您可以使用<$c$c>ErrorMessageResourceName属性:

You could use the ErrorMessageResourceName property:

[Required(ErrorMessageResourceName = "SomeResource")]
[StringLength(30, ErrorMessageResourceName = "SomeOtherResource")]
public string Name { get; set; }

您可以结帐这篇博客文章一个例子。

更新:

的Application_Start

DefaultModelBinder.ResourceClassKey = "Messages";

而在 Messages.resx 文件,你需要添加自定义错误消息。使用反射来看看 System.Web.Mvc System.ComponentModel.DataAnnotations 组件才能看到关键名称使用。

And in the Messages.resx file you need to add the custom error messages. Use Reflector to look at the System.Web.Mvc and System.ComponentModel.DataAnnotations assemblies in order to see the key names to use.

这篇关于本地化数据注释缺省消息([必需] [StringLength]等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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