views:

28

answers:

1

Our model has public DateTime date { get; set; }. In our view, our dates are being stored in the JSON date format. We're then trying to save updates to the date

var someObject = {};
someObject.date = JSONDate;
$.post("Controller/SaveAction", someObject, callback);

and our controller has public JsonResult SaveAction(ModelType model) { ... } but this code breaks because it is unable to convert the JSON date to a c# DateTime object.

How can I convert the JSON date to something that the post will controller will be able to correctly read into a C# DateTime object?

+2  A: 

Your best bet is probably using the JSON.NET library, where things like this have already been defined and (more or less) standardized.

http://json.codeplex.com/

Robert Harvey
Thanks, while this looks like it would solve my problem, I am unfortunately unable to add external dependencies to our project. I was hoping more for something I could do in javascript before assigning it to someObject.date.
blindworld
If you just need the part that converts to DateTime, you can probably "borrow" the code from the JSON.NET library (it's licensed under a BSD-like non-restrictive license), or write a work-alike. In particular, have a look at the `JavaScriptDateTimeConverter.cs` class.
Robert Harvey