asp.net mvc - pass empty string when user does not input anything in @HtmlTextboxFor -


facing problem have @htmltextboxfor when user doesnot insert returning error how pass empty string or null if left blank.

the parameters dictionary contains null entry parameter 'fromdate' of non-nullable type 'system.datetime'

when user doesnot insert pass empty string or null value otherwise value inserted user.

whats wrong code.

public class reportviewmodel     {         public string fromdate { get; set; }         public string todate { get; set; }     private tdbcontext tdbcontext;     private ireportservice reportservice;     public void viewreportlist(datetime fromdate, datetime todate)     {         reportservice = new reportservice(tdbcontext);         reportlist = reportservice.getreportslist(fromdate, todate);     }     } 

view

@model req.viewmodels.reportviewmodel @using (html.beginform("index", "print", formmethod.post)) {  @html.textboxfor(m => m.fromdate, new { @readonly = "readonly", @class = "date-picker form-control"}) @html.textboxfor(m => m.todate, new { @readonly = true, @class = "date-picker form-control"}) } 

index action

[httppost]         public actionresult index(reportviewmodel reportviewmodel,datetime fromdate, datetime todate)         { ... reportviewmodel.viewreportlist(fromdate, todate);                 return view("index", reportviewmodel);             } 

revised code after suggestion

[httppost]             public actionresult index(reportviewmodel reportviewmodel)             {     ...     reportviewmodel.viewreportlist(reportviewmodel.fromdate, reportviewmodel.todate);                     return view("index", reportviewmodel);                 } 

viewmodel

public class reportviewmodel         {             public datetime? fromdate { get; set; }         public datetime? todate { get; set; }         private tdbcontext tdbcontext;         private ireportservice reportservice;         public void viewreportlist(datetime fromdate, datetime todate)         {             reportservice = new reportservice(tdbcontext);             reportlist = reportservice.getreportslist(fromdate, todate);         }         } 

now getting error showing error

the best overloaded method match viewreportlist(system.datetime,system.datetime)

after changes.

the string field fromdate in vm initialized empty string anyways , not seem issue. issue here post method. model binder trying convert fromdate string datetime param , not optional according method signature.

if these params should optional, should specify making date params nullable:

public actionresult index(reportviewmodel reportviewmodel, datetime? fromdate, datetime? todate) 

or providing default value:

public actionresult index(reportviewmodel reportviewmodel, datetime fromdate = datetime.minvalue, datetime todate = datetime.maxvalue) 

however, have dates in viewmodel these params redundant.

my suggestion:

[httppost] public actionresult index(reportviewmodel reportviewmodel) {     ...     reportviewmodel.viewreportlist(reportviewmodel.fromdate, reportviewmodel.todate);     return view("index", reportviewmodel); } 

and change vm datetimes:

public class reportviewmodel {     public datetime fromdate { get; set; }  // maybe make these nullable or set defaults?     public datetime todate { get; set; }     ... } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -