0

My AJAX method returns a view model, which is a data structure serialized using JavaScriptSerializer().Serialize(). This data includes a number of nullable DateTime? properties.

It has come to my attention that these dates show up in JavaScript as a string in the format "/Date(1480551007625)/". After a little research, I figured out how to convert this to a JavaScript date.

But the problem is that if I post the data back to my AJAX methods, I get the following error:

/Date(1480551007625)/ is not a valid value for DateTime.

This error occurs even when I haven't modified that value in the view model! That is, I'm just posting back the exact same, unmodified view-model object.

How do I convert the DateTime properties in my view mode on the client side so that they can be posted back to the server and be converted back to DateTime properties.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • [This question](http://stackoverflow.com/questions/12482856/proper-way-to-convert-json-date-to-net-datetime-during-deserialization) may be of use – Rob Dec 01 '16 at 02:33

2 Answers2

0

I would guess you need to do something like

serializer.Deserialize<DateTime>("/Date(1480551007625)/");

without the <DateTime>, I'd expect Deserialize to return a string and converting a string in this format to a DateTime probably doesn't work.

Rob
  • 26,989
  • 16
  • 82
  • 98
Paul Buis
  • 805
  • 7
  • 7
  • The error occurs when it attempts to parse the data to my C# data structures. So there is an error before my C# code even gains control here. – Jonathan Wood Dec 01 '16 at 02:31
0

To my surprise, this is waaaaaaaaaay too much trouble. So, in my view model, I changed the date fields to strings and just stored the strings in the format "2016|12|1".

Problem solved.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466