0

I use the jquery ui controls.
I have a datepicker and I retrieve the date as follows:

var sDate = $("#startDatePicker").datepicker('getDate'); 
var eDate = $("#endDatePicker").datepicker('getDate');

which as an example for sDate returns

Tue Aug 14 00:00:00 UTC+0200 2012

I call the code from the web page as follows

$.ajax(
{
  url: '@Url.Action("LogsView","Home")',
  type: 'POST',
  data: { startDate: sDate, endDate: eDate },
  success: function(result) { alert(result); },                        
  error: function(param1) { alert(param1); }
});

I have a controller with the following action

public JsonResult LogsView(DateTime startDate, DateTime endDate)
{   
  var items = FetchItems(startDate, endDate);
  return Json(items, JsonRequestBehavior.AllowGet);
}

When I run it, part of the error that is returned is as follows (when vied in fiddler):

The parameters dictionary contains a null entry for parameter 'startDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.JsonResult LogsView(System.DateTime, System.DateTime)' ..

I've check my params sent via fiddler and it is as follows startDate=&endDate=

Anyone know how to format and pass in the date correctly (I assume this is where the error lies)?

3 Answers 3

4

I needed to add .toJSON() to $("#startDatePicker").datepicker('getDate')
i.e. $("#startDatePicker").datepicker('getDate').toJSON()

if you get an error on the toJSON() function then try

var sd = $("#startDatePicker").datepicker('getDate');
var sDate = $.datepicker.formatDate('dd-MM-yy', sd);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert the date to string before you send it off to $.ajax() method.

Comments

0

You can this method to convert date to string:

function ShowUTCDate()
{
    var dNow = new Date();
    var utc = new Date(dNow.getTime() + dNow.getTimezoneOffset() * 60000);
    var utcdate= (utc.getMonth()+1) + '/' + utc.getDate() + '/' + utc.getFullYear() + ' ' + utc.getHours() + ':' + utc.getMinutes();
    return utcdate;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.