facing a problem i have a @HtmlTextboxFor when user doesnot insert anything it is returning the error how to pass empty string or null if it left blank.
The parameters dictionary contains a null entry for parameter 'FromDate' of non-nullable type 'System.DateTime'
when user doesnot insert anything it pass a empty string or null as value otherwise the value inserted by User.
whats wrong with my 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 i am getting this error it is showing the error
the best overloaded method match for ViewReportList(System.DateTime,System.DateTime)
after changes.
reportViewModel.ViewReportListmethod?MM/dd/yyyy?dateFormat: 'mm/dd/yy'DateTime. If you want to be able to pass back anullvalue, make the properties in your modelDateTime?(nullable) NOT strings!. The second is because your using sqldatetimewhen you need to use sqldatetime2if you want the full range