当用户没有在@HtmlTextboxFor中输入任何内容时传递空字符串

时间:2015-06-10 15:42:57

标签: asp.net-mvc razor html.textboxfor

遇到问题我有一个@HtmlTextboxFor当用户没有插入任何东西它返回错误如何传递空字符串或null如果它留空。

  

参数字典包含参数的空条目   ' FROM日期'非可空类型&Systems.DateTime'

当用户不插入任何内容时,它会传递一个空字符串,或者为null,否则为User插入的值。

我的代码有什么不对。

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);
                }

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);
        }
        }

现在我收到此错误,显示错误

  

最佳重载方法匹配   ViewReportList(System.DateTime的,的System.DateTime)

更改后。

2 个答案:

答案 0 :(得分:1)

无论如何,VM中的字符串字段FromDate将初始化为空字符串,似乎不是问题。这里的问题是你的POST方法。模型绑定器正在尝试将FromDate字符串转换为param的日期时间,并且根据方法签名它不是可选的。

如果这些参数应该是可选的,您应该通过使日期参数可以为空来指定:

public ActionResult Index(ReportViewModel reportViewModel, DateTime? FromDate, DateTime? ToDate)

或提供默认值:

public ActionResult Index(ReportViewModel reportViewModel, DateTime FromDate = DateTime.MinValue, DateTime ToDate = DateTime.MaxValue)

但是,您的viewmodel中已有日期,因此这些参数是多余的。

我的建议:

[HttpPost]
public ActionResult Index(ReportViewModel reportViewModel)
{
    ...
    reportViewModel.ViewReportList(reportViewModel.FromDate, reportViewModel.ToDate);
    return View("Index", reportViewModel);
}

并将VM本身更改为DateTimes:

public class ReportViewModel
{
    public DateTime FromDate { get; set; }  // maybe make these nullable or set defaults?
    public DateTime ToDate { get; set; }
    ...
}

答案 1 :(得分:0)

尝试使用默认值

@ Html.TextBoxFor(m => m.FromDate,new {@readonly =“readonly”,@ class =“date-picker form-control”, Value =“”})