将DataTime作为对象JSON的一部分传递

时间:2016-02-01 08:08:51

标签: c# json asp.net-mvc

我正在尝试通过Ajax将数据时间发送到Controller。

var toDbViewModel = {
    "ClientId": clientId,
    "ProjectId": projectId,
    "TaskId": taskId,
    "Description": description,
    "Duration": totaldur,
    "Start": start.toISOString()
}

其他每个变量都可以,但“开始”等于“1/1/0001 12:00 AM”

我做错了什么?

- 编辑 -

从网站发送时,日期为:“2016-01-04”

2 个答案:

答案 0 :(得分:1)

我认为,您在服务器端获得字符串“1/1/0001 12:00 AM”,因为C#无法解析您传递的日期时间值并采用默认的DateTime值。

以下是将JSON数据传递给服务器的示例示例。

服务器端:

public class Person
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public string[] Hobbies { get; set; }
}

方法:

public bool ProcessData(Person person)
{
    return person != null;
}

客户端:

//POST the following JavaScript
var personObj = {
    name: "ABC",
    birthday: new Date(2016, 0, 1),
    hobbies: ["xyz", "pqr"]
};

$.ajax({
    url: "URL",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({ person: personObj }),
    success: function(response) {
        response ? alert("It worked!") : alert("It didn't work.");
    }
});

答案 1 :(得分:0)

您可以将日期作为时间戳发送, 在Java>> http://www.tutorialspoint.com/java/util/date_gettime.htm

相关问题