开发者

deserialize javascript date on server asp.net mvc 3

开发者 https://www.devze.com 2023-02-14 05:45 出处:网络
I\'ve been struggling with this issue for a while. I\'m trying to send an object to the client. Once the client updates the object I\'d like to post it back to the server and save it in my database. T

I've been struggling with this issue for a while. I'm trying to send an object to the client. Once the client updates the object I'd like to post it back to the server and save it in my database. The first part works fine. However, when I send it back all the dates are messed up and start for year 0001. I assume that's because it can't deserialize the date. Howe can I post a json object with a date property and deserialize it into a type on the server with asp.net mvc 3?

group is

public class Group
{
    public G开发者_如何学JAVAroup();

    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
    public string Description { get; set; }
    public string Name{ get; set; }
    public string Status { get; set; }
    public string UpdatedBy { get; set; }
    public DateTime UpdatedOn { get; set; }
}

while

    public JsonResult updateGroup(Group group)
    {
        var result = Repository.updateGroup(group);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

and on the client I got

    $.ajax({
        url: '../updateGroup',
        type: 'POST',
        data: JSON.stringify(group),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function(){ success(); }
    });


ASP.NET MVC 3 uses the JavaScriptSerializer to deserialize values back. Quoting from the documentation of this class:

Date object, represented in JSON as "\/Date(number of ticks)\/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).

This basically means that your request object has to look something like this:

{ CreatedOn: '\/Date(1299098853123)\/', CreatedBy: 'foo', ... }

So here's how you could proceed:

// taking the local time but this could be any javascript Date object
var now = new Date(); 
var group = {
    CreatedBy: 'foo',
    CreatedOn: '\/Date(' + now.getTime() + ')\/',
    Description: 'some description',
    Name: 'some name',
    etc...
};

Of course because we are now working with UTC don't forget to compensate to local time on your server if you ever need to work with local time (UTC is preferred):

public ActionResult UpdateGroup(Group group)
{
    DateTime serverCreatedOn = group.CreatedOn.ToLocalTime();
    ...
}

Also don't hardcode urls like this:

url: '../updateGroup'

Always use URL helpers when dealing with urls or you might get bad surprises when you ship:

url: '@Url.Action("updateGroup")'

UPDATE:

Try encoding the date like this:

// taking the local time but this could be any javascript Date object
var now = new Date(); 
var group = {
    CreatedBy: 'foo',
    CreatedOn: now.toUTCString(),
    Description: 'some description',
    Name: 'some name',
    etc...
};


Thanks for your help Darin. the UTC string didn't quite work but I got this to work

    var now = new Date();
    group.CreatedOn = now.toLocaleString();

and even

    var now = new Date();
    group.CreatedOn = now;

works

0

精彩评论

暂无评论...
验证码 换一张
取 消