面临一个问题,当用户没有插入任何内容时,我有一个@HtmlTextboxFor,它返回错误,如果它留空,则如何传递空字符串或 null。
参数字典包含不可为空类型“System.DateTime”的参数“FromDate”的空条目
当用户不插入任何内容时,它传递一个空字符串或 null 作为值,否则传递用户插入的值。
我的代码有什么问题。
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);
}
}
看法
@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"})
}
索引操作
[HttpPost]
public ActionResult Index(ReportViewModel reportViewModel,DateTime FromDate, DateTime ToDate)
{
...
reportViewModel.ViewReportList(FromDate, ToDate);
return View("Index", reportViewModel);
}
建议后修改代码
[HttpPost]
public ActionResult Index(ReportViewModel reportViewModel)
{
...
reportViewModel.ViewReportList(reportViewModel.FromDate, reportViewModel.ToDate);
return View("Index", reportViewModel);
}
视图模型
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);
}
}
现在我收到此错误,它显示错误
ViewReportList(System.DateTime,System.DateTime) 的最佳重载方法匹配
更改后。