In a MVC4 application, I'm passing my view model to the controller action method AsyncUpdateOrderLine:
function updateQuantity(sender) {
...
var model = @Html.OrderToJson(Model)
$.ajax({ type: "POST",
url: "/Order/AsyncUpdateOrderLine",
datatype: "json",
contentType: 'application/json',
data: JSON.stringify({ orderLineId: orderLineId, quantity: quantity, myOrder: model }),
success: function (msg) {
...
}
});
}
When I check the myOrder parameter in runtime, I notice that the DateTime properties (such as OrderDate) of my model are not deserialized properly: 1/1/0001. Other properties in the deserialized model look fine. My OrderToJson method looks like this:
public static MvcHtmlString OrderToJson(this HtmlHelper html, MyViewModel viewModel)
{
var json = new JavaScriptSerializer().Serialize(viewModel);
return MvcHtmlString.Create(json);
}
I already tried to set the Kind of the DateTime properties to Utc, but this doesn't do anything for me. I also made a small console application to test serializing and deserializing DateTime properties. Needless to say, in that sample app there's no issue. It might be related to the way that MVC deserializes the JSON string. Any tips to solve this issue?
Regards, Nils