2

I want to compare two date properties in mvc application with check boxes ,My return date should not less than departure date I am getting error "_RetDate is not a valid named attribute argument because it is not a valid attribute parameter type"

here is my code

    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; }

here is my custom attribute class

  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;
    }

}
Ravi Hanok
  • 405
  • 1
  • 12
  • 23

1 Answers1

1

I think you are passing the return date the wrong way. This may be of help: Custom model validation of dependent properties using Data Annotations.

Community
  • 1
  • 1
meilke
  • 3,280
  • 1
  • 15
  • 31
  • Hi meilke ,I have seen ur link ,I couldn't find matching logic ,could you please review the code and correct it, where is wrong in my code – Ravi Hanok Sep 18 '13 at 05:54
  • I am not a pro in this regard. But from **a quick Google search** I found that you might have to use a) the base constructor and b) property names as strings. Your code seems to have problems parsing/casting the string `"ReturnDate"` to `DateTime`. – meilke Sep 18 '13 at 05:58