在DataAnnotation如何本地化的ErrorMessage? [英] How localize ErrorMessage in DataAnnotation?

查看:473
本文介绍了在DataAnnotation如何本地化的ErrorMessage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC 5,我需要一个DataAnnotation属性本地化的ErrorMessage。
我收到以下错误

Using MVC 5 I need to localize an ErrorMessage for a DataAnnotation attributes. I receive the following error

错误

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

在模型

[Compare("Password", ErrorMessage = Resources.Account_Register_ConfirmPasswordErrorMessage)]
public string ConfirmPassword { get; set; }

不知道如何解决它?

Any idea how to fix it?

推荐答案

您需要使用 ErrorMessageResourceName ErrorMessageResourceType 属性。

例如这样的:

[Compare("Password", ErrorMessageResourceName = "ConfirmPasswordErrorMessage", ErrorMessageResourceType=typeof(<<type_of_your_resoruce_class>>)]
public string ConfirmPassword { get; set; }

下面也是MSDN,在这里你可以找到两个属性解释的链接。

Here is also a link to MSDN, where you can find explanation for both properties.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.compareattribute(v=vs.110).aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.compareattribute(v=vs.110).aspx

希望这有助于!

问候,
乌罗什

Regards, Uros

这篇关于在DataAnnotation如何本地化的ErrorMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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