1

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.

17
  • It's still not clear where the error happens, can you add the stack trace and the code of reportViewModel.ViewReportList method? Commented Jun 10, 2015 at 15:57
  • if i left it blank it does not comes to the Index method for debugging. Commented Jun 10, 2015 at 15:59
  • @Miranda what's the date format of the date picker? Is it MM/dd/yyyy? Commented Jun 10, 2015 at 16:11
  • @ekad Date Format is dateFormat: 'mm/dd/yy' Commented Jun 10, 2015 at 16:13
  • 1
    Because that's the default value for DateTime. If you want to be able to pass back a null value, make the properties in your model DateTime? (nullable) NOT strings!. The second is because your using sql datetime when you need to use sql datetime2 if you want the full range Commented Jun 11, 2015 at 9:37

2 Answers 2

1

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; }
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

i have updated m code accrding to your suggestion but its giving me strange value ` 1/1/0001 12:00:00 AM` when i left it blank, while debugging.. and then its throw the error SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Are any fields going to be optional? If so, then make them nullable. If not, then mark them as [Required] or set a default value so it doesnt error. 1/1/0001 is the default value of a datetime so that means it was not set anywhere in your code
0

Try using default value

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.