在RangeAttribute中提供非常量值?

nee*_*ebz 4 c# asp.net-mvc data-annotations

当我执行以下操作时:

`[Range(1910, DateTime.Now.Year)]  
public int Year { get; set; }`
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

有什么想法吗?

moi*_*eme 6

您可以为此构建一个自定义属性,就像RangeYearToCurrent您指定当前年份一样

public class RangeYearToCurrent : RangeAttribute
{
    public RangYearToCurrent(int from)
        : base(typeof(int), from, DateTime.Today.Year) { }
}
Run Code Online (Sandbox Code Playgroud)

未经测试...