c# - 在 RangeAttribute 中提供非常量值?

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

当我执行以下操作时:

`[Range(1910, DateTime.Now.Year)]  
public int Year { get; set; }`

我收到以下错误:

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

有什么想法吗?

最佳答案

您可以为此构建一个自定义属性,例如 RangeYearToCurrent,您可以在其中指定当前年份

public class RangeYearToCurrent : RangeAttribute
{
    public RangYearToCurrent(int from)
        : base(typeof(int), from, DateTime.Today.Year) { }
}

未经测试...

关于c# - 在 RangeAttribute 中提供非常量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4890310/

相关文章:

c# - ASP MVC 应用程序在长时间运行的线程上重置

jquery - Json结果问题

c# - 表单中不存在的所需实体验证失败?

c# - 单独类库中数据注释的本地化

c# - UWP - ListView 垂直 ScrollView 已更改

c# - 在 Linq 的循环中添加 OR 表达式

c# - c#中逐字字符串的多行格式(前缀为@)

c# - 将字符串拆分为字典作为键,列表

asp.net-mvc - MVC Validator.TryValidateObject 不验证自定义属性,validateAllProperties = true

data-annotations - 使用数据注释设置由 LoadFromCollection (epplus) 生成的单元格的格式