I have a knockout/mvc3 application. I am passing the date back to a controller.
controller
public ActionResult PackageUpdate(Package updatePackage){
\\do some stuff but dates are set to zero?
}
view model and save method
var Package = function (data) {
self = this;
self = ko.mapping.fromJS(data);
self.save = function(){
$.ajax({
url: '/MediaSchedule/PackageUpdate',
data:ko.toJSON({ updatePackage: self })
}).success(function (results) {
console.log(results);
}).error(function (er) {
console.error('Ah damn you broke it.')
console.log(er);
});
}
return self;
}
Json Being passed.
{"updatePackage":{"Id":"82e3bc7e-27b8-49c2-b1fa-1ee2ebffbe66","Name":"28a38","SecondaryName":"è€å我è¦é’±","IsLocked":true},"DateCreated":"/Date(1357650000000+1100)/","DateStart":"/Date(1365080400000+1100)/","DateEnd":"/Date(1365516000000+1000)/"}
ID, name and other properties are coming through but the date is being reset to {1/1/0001 12:00:00 AM}. My assumption is because it is not being deserialised it is setting a min date. Question: How do I correctly deserialise my date.
Thanks to Matt Johnson I was able to change the way the dates were being sent to the browser. It was a relativly easy fix taken from Perishable Dave answer to a similar problem with ASP.NET MVC JsonResult Date Format
My JsonNetResult class now look like
I have added the iso date converter to the serizer
in the controller you invoke it by:
When I wrote this I did not know about Web API. It is worth a look as it will do a lot of the heavy lifting when it comes to serialization of your objects. Checkout Getting Started with ASP.NET Web API 2
I think it just the data type you push to you updatePackage object. Following is my code and run well, I use read the Date from jQuery Datepicker and use the format as 'dd MM, yy' (01 January 2013)
and my ActionResult
Hope this help Regard
I think the problem lies in how you obtaining those dates to begin with. You show an example using MS date format such as
/Date(1357650000000+1100)/
which is not standardized and is slowly being deprecated in favor of ISO8601, which looks like2013-01-08T13:00:00.000+11:00
.Indeed, when you
JSON.stringify
a javascriptDate
object, it uses the ISO8601 format. This also happens withko.mapping.toJSON
.There are several solutions to this problem, both client side and server side. This post describes the problem in detail and has some great answers that may help you.
IMHO, the best solution is have your MVC controller emit and consume ISO8601 rather than the old Microsoft date format. The easiest way to do that is by using the Json.Net library which now has ISO8601 as the default so you don't even need to customize it. On the client side, you might also want to look at Moment.js - which makes it easy to parse and format ISO dates.