c# - ASP.NET MVC4 : An attribute argument must be a constant expression , 属性参数类型的typeof表达式或数组创建表达式

标签 c# asp.net-mvc asp.net-mvc-4 data-annotations

我有以下代码:

    [Required(ErrorMessage = MessageModel.translateMessage("required")))]
    [Display(Name= MessageModel.translateMessage("id"))]
    public string user_id { get; set; }

我正在尝试使错误消息动态化,但在编译时出现错误。:

“属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式。”

这个问题有什么解决办法吗?

最佳答案

首先您创建一个 Resource .resx 文件,它将包含您的本地化字符串。

当您声明属性时,您设置了 ResourceType 参数。这会导致 Name、ShortName 和 Description 参数被用作资源键而不是值。

[Display(Name = "GenreName", ShortName = "GenreShortName", Description = "GenreDescription", ResourceType = typeof(MyResources))]
public string Genre { get; set; }

关于c# - ASP.NET MVC4 : An attribute argument must be a constant expression , 属性参数类型的typeof表达式或数组创建表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14999117/

相关文章:

IOS 到 REST-ful MVC Api 服务 - [Get] 有效,但参数无法通过 [Post] 到达服务器

javascript - 将编码后的字符串发送回服务器 asp.net mvc

c# - 从 cookie 创建和读取列表<>

c# - 如何使用图标呈现数据绑定(bind)的 WinForms DataGridView 列?

c# - 有限并发级别的任务调度程序(具有任务优先级)处理包装任务

asp.net-mvc - ASP.NET MVC 和 AngularJS 路由

c# - ASP.NET MVC 5 路由可选参数

c# - 如何在 ASP.NET MVC 4.0 中向 List<T> 添加数据?

C# Activator.CreateInstance 错误

c# - 静态变量变为空