Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在注释ErrorMessages(C#MVC 4)上使用常量和格式化字符串_C#_Asp.net Mvc_Annotations - Fatal编程技术网

如何在注释ErrorMessages(C#MVC 4)上使用常量和格式化字符串

如何在注释ErrorMessages(C#MVC 4)上使用常量和格式化字符串,c#,asp.net-mvc,annotations,C#,Asp.net Mvc,Annotations,好的,在这一点上,我只是试图优化我的代码以备将来可能的维护,因此,在实体定义上,我试图将属性(和其他注释)的字符串大小集中在一个点上 下面是一个例子: // ---=== CONSTS ===--- protected const int Nome_MaxLength = 120; protected const int Sigla_MaxLength = 4; public int IDTipoServico { get; set; } [Displ

好的,在这一点上,我只是试图优化我的代码以备将来可能的维护,因此,在实体定义上,我试图将属性(和其他注释)的字符串大小集中在一个点上

下面是一个例子:

    // ---=== CONSTS ===---
    protected const int Nome_MaxLength = 120;
    protected const int Sigla_MaxLength = 4;

    public int IDTipoServico { get; set; }

    [Display(Name = "Tipo Serviço")]
    [StringLength(Nome_MaxLength, ErrorMessage = String.Format("O nome de um tipo de serviço deve conter no máximo {0} caracteres", Nome_MaxLength))]
    public string Nome { get; set; }

    [Display(Name = "Sigla Tipo Serviço")]
    [StringLength(Sigla_MaxLength, ErrorMessage = String.Format("A sigla de um tipo serviço deve conter no máximo {0} caracteres", Sigla_MaxLength))]
    public string Sigla { get; set; }
尽管它看起来简单明了,但它不会起作用。。。编译器给了我“属性参数必须是常量表达式、typeof表达式或属性参数类型的数组创建表达式”错误


有什么想法吗?

我已经看到通过围绕默认属性的包装器类实现了这一点。这是你的一个选择(现在想不出其他任何事情)。那就可以了。。。你能举个例子吗(因为我不能正确地做)。但我确实认为这是一种方式……如果您愿意使用库,可能会给您比编写自己的属性更大的灵活性。