ko.toJSON() 可以处理日期吗?

发布于 2024-11-05 17:27:42 字数 1588 浏览 1 评论 0原文

我在 ASP.NET MVC 页面上使用 KnockoutJS。我使用 ajax 通过调用 ko.toJSON(viewModel) 来将表单保留回服务器,然后使用 jQuery 将结果发布回服务器。视图模型上的所有属性均已成功序列化,但 Javascript 日期除外,该日期保留为空对象。

声明:

var viewModel = {
    startTime: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

保存数据:

var postData = ko.toJSON(viewModel); 
$.ajax({
    url: "/data",
    type: "POST",
    data: postData,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function () {
        console.log('success!');
    },
    error: function () {
        console.log('fail!');
    }
});

viewModel.startTime() 的 console.log 值为:

日期 {2011 年 5 月 10 日星期二 11:30:00 GMT-0500(中部夏令时间)}

保存数据第 1 行之后,postData 的值为:

{
    "startTime": {},
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

如果我展开保存数据的第 1 行到

var jsonEvent = ko.toJS(viewModel);
jsonEvent.startTime = viewModel.startTime();
var postData = JSON.stringify(jsonEvent);

postData 的值是:

{
    "startTime": "2011-05-10T16:30:00.000Z",
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

任何人都可以解释可能发生的情况以及我如何能够让 knockoutjs 处理日期对象?

I am using knockoutjs on an asp.net mvc page. I am using ajax to persist a form back to the server by calling ko.toJSON(viewModel) and then posting the results back to the server using jQuery. All of the properties on the view model are successfully serialized except for the Javascript date which is persisted as an empty object.

Declaration:

var viewModel = {
    startTime: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

Save Data:

var postData = ko.toJSON(viewModel); 
$.ajax({
    url: "/data",
    type: "POST",
    data: postData,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function () {
        console.log('success!');
    },
    error: function () {
        console.log('fail!');
    }
});

The console.log value of viewModel.startTime() is:

Date {Tue May 10 2011 11:30:00 GMT-0500 (Central Daylight Time)}

After line 1 of Save Data, the value of postData is:

{
    "startTime": {},
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

If I expand line 1 of Save Data to

var jsonEvent = ko.toJS(viewModel);
jsonEvent.startTime = viewModel.startTime();
var postData = JSON.stringify(jsonEvent);

The value of postData is:

{
    "startTime": "2011-05-10T16:30:00.000Z",
    "type": "1",
    "durationInMinutes": "45",
    "notes": "asfasdfasdfasdfasdfasdfasdfas",
    "displayableStartTime": "10-May 11:30"
}

Can anyone explain what might be going on and how I might be able to get knockoutjs to handle the date object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-11-12 17:27:42

鉴于 ko.toJS 和日期的当前问题,一种选择是创建一个 dependentObservable ,其中包含您希望服务器处理的实际值。

类似于:

var viewModel = {
    startTimeForInput: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

viewModel.startTime = ko.dependentObservable(function() {
    return this.startTimeForInput().toJSON();
}, viewModel);

ko.applyBindings(viewModel);

现在,当您调用 ko.toJSON 时,您将获得带有服务器可以使用的正确值的 startTime 。

对于较旧的浏览器,诸如 json2.js 之类的内容将包含 Date 对象的 .toJSON。

Given the current issue with ko.toJS and dates, one option would be to create a dependentObservable containing the real value that you want the server to deal with.

Something like:

var viewModel = {
    startTimeForInput: ko.observable(),
    type: ko.observable(),
    durationInMinutes: ko.observable(),
    notes: ko.observable()
};

viewModel.startTime = ko.dependentObservable(function() {
    return this.startTimeForInput().toJSON();
}, viewModel);

ko.applyBindings(viewModel);

Now, when you call ko.toJSON you will get the startTime with the correct value that the server could use.

For older browsers, something like json2.js would include the .toJSON for Date objects.

山田美奈子 2024-11-12 17:27:42

当日期为 DateTime.MinValue 时,我遇到了 ko.toJSON() 给我提供错误日期格式的问题。

虽然可能无法解决您的问题,但此修复适用于我的 ko.toJSON() 日期问题:

var postData = JSON.parse(JSON.stringify(ko.toJSON(viewModel)).replace(/\"1-01-01/g, "\"0001-01-01"));

ASP.Net WebMethod 失败,因为 ko.toJSON() 为 DateTime.MinValue 生成不同的结果

I had a problem with ko.toJSON() giving me a bad date format when the date was DateTime.MinValue.

Though probably not a fix for your problem, this fix worked for my ko.toJSON() date problem:

var postData = JSON.parse(JSON.stringify(ko.toJSON(viewModel)).replace(/\"1-01-01/g, "\"0001-01-01"));

ASP.Net WebMethod fails because ko.toJSON() produces different results for DateTime.MinValue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文