为什么我不能用资源的ErrorMessage与DataAnnotations? [英] Why can't I use resources as ErrorMessage with DataAnnotations?

查看:193
本文介绍了为什么我不能用资源的ErrorMessage与DataAnnotations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能做这样的?

  [必需(的ErrorMessage =*)]
[RegularEx pression(^ [A-ZA-Z0-9 _] * $的ErrorMessage = Resources.RegistrationModel.UsernameError)
公共字符串用户名{获得;组; }

什么是错误消息告诉我?


  

这是属性参数必须是
  恒恩pression的typeof
  前pression或数组创建
  一个属性参数的前pression
  类型。



解决方案

当您使用的ErrorMessage 属性唯一不变的字符串或字符串文字可以被分配给它。

使用 ErrorMessageResourceType ErrorMessageResourceName 来代替specity你的资源。

  [RegularEx pression(
    ^ [A-ZA-Z0-9 _] * $
    ErrorMessageResourceType = typeof运算(Resources.RegistrationModel)
    ErrorMessageResourceName =UsernameError
)]

请注意,该资源必须公开(可以在资源编辑器中设置)。

Why can't I do like this?

[Required(ErrorMessage = "*")]
[RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)]
public string Username { get; set; }

What is the error message telling me?

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

解决方案

When you are using the ErrorMessage property only constant strings or string literal can be assigned to it.

Use the ErrorMessageResourceType and ErrorMessageResourceName instead to specity your resources.

[RegularExpression(
    "^[a-zA-Z0-9_]*$", 
    ErrorMessageResourceType=typeof(Resources.RegistrationModel),
    ErrorMessageResourceName= "UsernameError"
)]

Note that the resources must be public (can be set in the resource editor).

这篇关于为什么我不能用资源的ErrorMessage与DataAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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