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

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

问题描述

如果我用这样的属性装饰我的 ViewModel 的属性:

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"

我该如何翻译它们?

推荐答案

您可以使用 ErrorMessageResourceName 属性:

You could use the ErrorMessageResourceName property:

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

您可以查看 此博客发帖 举个例子.

You may checkout this blog post for an example.

更新:

Application_Start中:

DefaultModelBinder.ResourceClassKey = "Messages";

并且在 Messages.resx 文件中您需要添加自定义错误消息.使用 Reflector 查看 System.Web.MvcSystem.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天全站免登陆