asp.net - Custom ValidationAttribute to check invalid DateTime -


i want write custom validator web api project check whether input date valid. please find below validator wrote.

validator:

   public sealed class datevalidationattribute : validationattribute {     public string datestring { get; set; }     public datevalidationattribute(string datestring)     {         datestring = datestring;     }      public override string formaterrormessage(string name)     {         return string.format(errormessagestring, name, datestring);     }      protected override validationresult isvalid(object value, validationcontext validationcontext)     {         var property = validationcontext.objecttype.getproperty(datestring);         datetime dateobject;         if (property != null && !datetime.tryparse(value.tostring(), out dateobject))         {             return new validationresult(                 formaterrormessage(validationcontext.displayname)             );         }         return null;     } } 

model:

public class testmodel {     [datevalidation("dateofbirth", errormessage = "date of birth not valid date")]     public datetime? dateofbirth { get; set; } } 

but if input invalid date, value in validator coming null (i assume because of model binding exception when tries convert value date). valid dates see value coming correctly. can't give null check in validator model property optional field. not supposed change datatype of field "string"

can 1 me find way solve retaining data type datetime?

note: please apologies me if there mistakes in question newbie .net technologies.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -