ASP.NET MVC4:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式 [英] ASP.NET MVC4: An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type

查看:97
本文介绍了ASP.NET MVC4:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

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

I am trying to make the error message dynamic but I get error when compiled.:

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

对此问题有解决方案吗?

Any solution for this issue?

推荐答案

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

First you create a Resource .resx file this will contain your localised strings.

声明属性时,设置ResourceType参数.这会导致将Name,ShortName和Description参数用作资源键而不是值.

When you declare the attribute you set the ResourceType argument. This causes the Name, ShortName and Description arguments to be used as a resource key instead of a value.

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

这篇关于ASP.NET MVC4:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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