asp.net mvc - using foolproof to make sure end datetime is greater than start datetime -


i using foolproof in mvc project. having trouble seems doing textual comparison figure out whether end datetime greater start datetime.

here model:

public class testmodel {     [required]     [datatype(datatype.datetime)]     public datetime start { get; set; }      [required]     [datatype(datatype.datetime)]     [greaterthan("start")]     public datetime end { get; set; } } 

the view

@model webapplication1.models.testmodel  @{     viewbag.title = "home page"; }   @using (html.beginform("index2", "home", formmethod.post, new { @class = "form-horizontal", role = "form" })) {     @html.antiforgerytoken()      @html.validationsummary("", new { @class = "text-danger" })     <div class="form-group">         @html.labelfor(m => m.start, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.start, "{0:dd/mm/yyyy hh:mm}", new { @class = "form-control datetimepicker" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.end, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.end, "{0:dd/mm/yyyy hh:mm}", new { @class = "form-control datetimepicker" })         </div>     </div>     <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input type="submit" value="set password" class="btn btn-default" />         </div>     </div> }  @section scripts {     @scripts.render("~/bundles/jqueryval") } 

the controller

    public actionresult index()     {         testmodel model = new testmodel();         // model.start = new datetime(2016, 5, 31);         //model.end = new datetime(2016, 8, 31);         model.start = datetime.now;         model.end = datetime.now.addminutes(30);         return view(model);     } 

you can download project try out: https://www.dropbox.com/s/pf0lkg297hq0974/webapplication1.zip?dl=0

there 2 issues: 1) when make start date 11/07/2016 23:51 , end date 02/08/2016 00:21 validation error thinks end date less start date. seems textual comparison me.

2) if un-comment out 2 model.start , model.end date initialization statements invalid date when submitting.

note using bootstrap datetimepicker commented out bit initialises when document ready. thought had issue seems not. want have datetimepicker working well.

also note in australia date format dd/mm/yyyy

in html ur posting index2 change index convert string datetime in greaterthanattribute in application , compare

try have tested implementation

the controller

 public class testcontroller : controller {     // get: test      [httpget]     public actionresult index()     {          var model = new testmodel();         // model.start = new datetime(2016, 5, 31);         //model.end = new datetime(2016, 8, 31);         model.start = datetime.now;         model.end = datetime.now.addminutes(30);         return view(model);       }     [httppost]     public actionresult index(testmodel model)     {         if (modelstate.isvalid)         {              // model.start = new datetime(2016, 5, 31);             //model.end = new datetime(2016, 8, 31);             model.start = datetime.now;             model.end = datetime.now.addminutes(30);              return view("index", model);         }         return view("index", model);      } } 

your testmodel

public class testmodel {     [required]     [datatype(datatype.datetime)]     public datetime start { get; set; }      [required]     [datatype(datatype.datetime)]     [greaterthan("start", "your error message")]     public datetime end { get; set; } } 

your greaterthenattribute.cs

 public class greaterthanattribute : validationattribute, iclientvalidatable {      public string otherpropertyname;     public greaterthanattribute()     {     }     public greaterthanattribute(string otherpropertyname, string errormessage) : base(errormessage)     {         this.otherpropertyname = otherpropertyname;     }      protected override validationresult isvalid         (object value, validationcontext validationcontext)     {         validationresult validationresult = validationresult.success;         try         {             // using reflection can reference other date property, in example project start date             var containertype = validationcontext.objectinstance.gettype();             var field = containertype.getproperty(this.otherpropertyname);             var extensionvalue = field.getvalue(validationcontext.objectinstance, null);             var datatype = extensionvalue.gettype();              //var otherpropertyinfo = validationcontext.objectinstance.gettype().getproperty(this.otherpropertyname);             if (field == null)                 return new validationresult(string.format("unknown property: {0}.", otherpropertyname));             // let's check otherproperty of type datetime expect             if ((field.propertytype == typeof(datetime) ||                  (field.propertytype.isgenerictype && field.propertytype == typeof(nullable<datetime>))))             {                 datetime tovalidate = (datetime)value;                 datetime referenceproperty = (datetime)field.getvalue(validationcontext.objectinstance, null);                 // if end date lower start date, validationresult set false , return                 // formatted error message                 if (tovalidate.compareto(referenceproperty) < 1)                 {                     validationresult = new validationresult(errormessagestring);                 }             }             else             {                 validationresult = new validationresult("an error occurred while validating property. otherproperty not of type datetime");             }         }         catch (exception ex)         {             // stuff, i.e. log exception             // let go through upper levels, bad happened             throw ex;         }          return validationresult;     }      public ienumerable<modelclientvalidationrule> getclientvalidationrules         (modelmetadata metadata, controllercontext context)     {         var rule = new modelclientvalidationrule         {             errormessage = formaterrormessage(metadata.getdisplayname()),             validationtype = "isgreater",         };         rule.validationparameters.add("otherproperty", otherpropertyname);         yield return rule;     } } 

Comments