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

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.