22

I am using a jQuery calendar to display events, which is designed to pull data from the server. On innit the calendar fires off a AJAX request to get an array of events objects (json encoded). All good so far. However, this request includes a JSON encoded date and time (at leats my implimentation does). The code looks like this:

data: function (start, end, callback) {
        $.post('/planner/GetPlannerEvents', { test: "test", start: JSON.stringify(start), end: JSON.stringify(end) }, function (result) { callback(result); });
    }

The declaration for the GetPlannerEvents controller method looks like this:

public ActionResult GetPlannerEvents(DateTime start, DateTime end)

The problem is that asp.net mvc 2 cannot seem to automatically parse the json encoded datetime and as such complains that the start and end values are null.

Is there another method I should be using to pass the javascript dates to the server so that they may be parsed correctly?

Thanks,

2
  • Is there any reason you are using JSON.stringify? What are the values prior to that call? Commented Oct 14, 2010 at 16:11
  • regular javascript dates. I think the demo I was looking at used JSON.stringify, but really I suppose there is no real reason to use it. Commented Oct 14, 2010 at 21:32

4 Answers 4

50

You shouldn't be JSON encoding the dates with stringify because the default model binder doesn't expect JSON. Try this:

$.post('/planner/GetPlannerEvents', { start: start.toUTCString(), 
    end: end.toUTCString() }, function (result) {
    callback(result); 
});
Sign up to request clarification or add additional context in comments.

5 Comments

Again Darin, you make my day!
May be worth noting the controller expects a string parameter and not a DateTime.
I wish this answer had more votes. Kinda difficult to dig in the pile of passing date to MVC controller questions. Can't believe toUTCString() was the answer. Props!
Only problem I've found with this is if you are in west of GMT such as -4 then .toUTCString() produces a day one day behind what the user chose in the date picker. I.e. 1/3/2013 00:00:00 -4 GMT becomes 1/2/2013 20:00:00 UTC. My inelegant hack is someDate = new Date(someDate.getTime() + someDate.getTimezoneOffset()*60000); Better suggestions are welcome.
You also need .toUTCString().replace('UTC','GMT') if in IE for MVC to bind successfully.
9

Try to use date.toISOString() to pass data to server. It returns string in ISO8601 format. Also this method can be used to format dates for using in uri.

$.post('/planner/GetPlannerEvents', { start: start.toISOString(), 
    end: end.toISOString() }, function (result) {
    callback(result); 
});

Why toISOString is better than toUTCString?
toUTCString converts to human readable string in the UTC time zone.
toISOString converts to universal ISO format which allows to resolve issue with regional settings and different formats.

1 Comment

Totally agree that ISO is much more reliable and consistent than UTC across different machine with different localization settings.
0

The variations of date.toString did not work for me until I added json headers to the post. The resulting code is as follows:

var pstData = {
  begDate: date1.toUTCString(),
  endDate :  date2.toUTCString()
};

$.ajax({
  url:'url',
  type:'POST',
  data: JSON.stringify( pstData ),
  contentType: "application/json; charset=utf-8",
  dataType: "json",
})

Comments

-5

You need to use return type as 1JsonResult1s instead of 1ActionResult1s

your code goes somthing like this

public JasonResult(DateTime start, DateTime end) {
    //some logic
    return Json(); // you can pass any values within Json() with new keyword
}

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.