pass empty string when user does not input anything in @HtmlTextboxFor

1.4k views Asked by At

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.

2

There are 2 answers

0
Muhammad Bilal On

Try using default value

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

2
Brad C On

The string field FromDate in your VM will be initialized to the empty string anyways and does not seem to be the issue. The issue here is your POST method. The model binder is trying to convert the FromDate string to a datetime for the param and it is not optional according to the method signature.

If these params should be optional, you should specify by making the date params nullable:

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

or providing a default value:

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

However, you already have dates in your viewmodel so these params are redundant.

My suggestion:

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

and change the VM itself to DateTimes:

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