While playing around with MVC 4's new single page application tooling, I noticed that none of the examples I have found contains an example of a DateTime being updated back through the WebApi. I soon found out why.
I started by generating the standard SPA from the template provided. I then opened up TodoItem.cs and added a DateTime field. Then I generated the controller as instructed by the comments. (Without the datetime field, everything works just fine).
After everything generated, I started the app and navigated to the controller index (I called the controller "tasks"). I got the grid page with 0 records as expected and clicked on the add button. I was taken to the edit page as expected and entered some data including a date in my shiny new datetime field. Then clicked save.
An error was produced that said:
Server error: HTTP status code: 500, message: There was an error deserializing the object of type System.Web.Http.Data.ChangeSetEntry[]. DateTime content '01/01/2012' does not start with '/Date(' and end with ')/' as required for JSON.
It would appear that the tooling doesn't support DateTime yet. I'm sure I could go through and spend a bit of time figuring it out and getting it to work, but I thought I may find a bit of luck here with someone who has already fixed this problem and can provide insight.
Anyone already battled this?
Update: I am adding more information I have found since asking this. I tried using JSON.Net as my Formatter as suggested below. I think that will be the eventual solution, however, just doing as the poster below recommended is not enough.
When using the JSON.Net serializer, I get the following error:
This DataController does not support operation 'Update' for entity 'JObject'.
The reason is that JSON.Net doesn't fully populate the object that the formatter is trying to deserailize to (System.Web.Http.Data.ChangeSet).
The json that is sent in is:
[{"Id":"0",
"Operation":2,
"Entity":
{"__type":"TodoItem:#SPADateProblem.Models",
"CreatedDate":"/Date(1325397600000-0600)/",
"IsDone":false,
"Title":"Blah",
"TodoItemId":1},
"OriginalEntity":
{"__type":"TodoItem:#SPADateProblem.Models",
"CreatedDate":"/Date(1325397600000-0600)/",
"IsDone":false,
"Title":"Blah",
"TodoItemId":1}
}]
The built in Json Formatter is able to reconstitute this Json into a ChangeSet object with embeded TodoItem objects in the Entity and OriginalEntity fields.
Has anyone gotten JSON.Net to deserialize this properly?