【问题标题】:Custom Attributes in c# that compares 2 date properties in mvc applicationc# 中的自定义属性比较 mvc 应用程序中的 2 个日期属性
【发布时间】:2013-09-18 05:21:12
【问题描述】:

我想用复选框比较 mvc 应用程序中的两个日期属性,我的返回日期不应小于出发日期 我收到错误 "_RetDate 不是有效的命名属性参数,因为它不是有效的属性参数类型"

这是我的代码

    public bool OneWay { get; set; }
    public bool Return { get; set; }
    [Required]
    [Display(Name = "Departure Date")]
    [ReturnDatenotGreater(_RetDate = "ReturnDate",ErrorMessage="ReturnDate can't be less that departure date")]         
    public DateTime DepartureDate { get; set; }
    [Display(Name = "Return Date")]
    public DateTime ReturnDate { get; set; }

这是我的自定义属性类

  public class ReturnDatenotGreaterAttribute : ValidationAttribute
  {
    public ReturnDatenotGreaterAttribute(DateTime Returdate)
    {
        _RetDate = Returdate;
    }
    public DateTime _RetDate { get; set; }

    public override bool IsValid(object value)
    {
        var departuredate= (DateTime)value;

        if (departuredate >_RetDate)
        {
            return false;
        }

        else

        return true;
    }

}

【问题讨论】:

    标签: c# asp.net-mvc attributes data-annotations custom-attributes


    【解决方案1】:

    我认为您以错误的方式传递了返回日期。这可能会有所帮助:Custom model validation of dependent properties using Data Annotations

    【讨论】:

    • 嗨 meilke,我看到了你的链接,我找不到匹配的逻辑,请你检查代码并纠正它,我的代码哪里错了
    • 我不是这方面的专家。但是通过快速的 Google 搜索,我发现您可能必须使用 a) 基本构造函数和 b) 属性名称作为字符串。您的代码似乎在将字符串 "ReturnDate" 解析/转换为 DateTime 时遇到问题。
    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多