When using Angular JS to post back to the server with a complex object the datetime and datetime? values do not bind correctly. I have tried JSON.stringify to no avail. I have posted a related question though possibly it was too general. What I really need to know is how to correctly pass those dates in. What I am currently doing is using workaround in js to convert the dates but I would rather not do that and simply get the dates in the form I need them when in Angular and then pass back the correct values.
How do you bind to those datetime/datetime? values correctly? Please see the following code example and Fiddler post results.
C# Class:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public DateTime? ApprovedForSomething { get; set; }
}
Angular JS Controller:
function PersonController($scope, $http) {
$scope.getPerson = function () {
$http.get('../../Home/GetPerson/1').success(function (data) {
$scope.Person = data;
});
}
$scope.updateApprovedForSomething = function () {
$http.post('../../Home/UpdatePerson', { person: $scope.Person }).success(function (data) {
console.log(data);
});
}
}
Fiddler Post:
HTTP/1.1 200 OK Cache-Control: private Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNetMvc-Version: 4.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcbmlja1xkb2N1bWVudHNcdmlzdWFsIHN0dWRpbyAyMDEyXFByb2plY3RzXFZhbGlkYXRpb25UZXN0XEhvbWVcR2V0UGVyc29uXDE=?= X-Powered-By: ASP.NET Date: Wed, 16 Jan 2013 14:48:34 GMT Content-Length: 124
{"FirstName":"Bob","LastName":"Smith","BirthDate":"/Date(695573315098)/","ApprovedForSomething":"/Date(1358261315098)/"}
This is the result on the server side. The datetime binds to a new datetime value which is not correct and the datetime? is null.
