淘汰赛日期在发布到 mvc 控制器时被重置 [英] knockout dates being reset on post to mvc controller

查看:9
本文介绍了淘汰赛日期在发布到 mvc 控制器时被重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个淘汰赛/mvc3 应用程序.我正在将日期传递回控制器.

I have a knockout/mvc3 application. I am passing the date back to a controller.

控制器

public ActionResult PackageUpdate(Package updatePackage){
\do some stuff but dates are set to zero?
}

查看模型和保存方法

var Package = function (data) {
    self = this;
    self = ko.mapping.fromJS(data);
    self.save = function(){
        $.ajax({
            url: '/MediaSchedule/PackageUpdate', 
            data:ko.toJSON({ updatePackage: self })
        }).success(function (results) {
            console.log(results);
        }).error(function (er) { 
            console.error('Ah damn you broke it.')
            console.log(er);
        });
    }
    return self;
}

Json 被通过.

{"updatePackage":{"Id":"82e3bc7e-27b8-49c2-b1fa-1ee2ebffbe66","Name":"28a38","SecondaryName":"è€å­æˆ‘è¦é’±","IsLocked":true},"DateCreated":"/Date(1357650000000+1100)/","DateStart":"/Date(1365080400000+1100)/","DateEnd":"/Date(1365516000000+1000)/"}

ID、姓名和其他属性正在通过,但日期被重置为 {1/1/0001 12:00:00 AM}.我的假设是因为它没有被反序列化它正在设置一个最小日期.问题:如何正确反序列化我的日期.

ID, name and other properties are coming through but the date is being reset to {1/1/0001 12:00:00 AM}. My assumption is because it is not being deserialised it is setting a min date. Question: How do I correctly deserialise my date.

推荐答案

我认为问题在于您如何获得这些日期.您展示了一个使用 MS 日期格式的示例,例如 /Date(1357650000000+1100)/ 这不是标准化的,并且正在慢慢被弃用以支持 ISO8601,它看起来像 2013-01-08T13:00:00.000+11:00.

I think the problem lies in how you obtaining those dates to begin with. You show an example using MS date format such as /Date(1357650000000+1100)/ which is not standardized and is slowly being deprecated in favor of ISO8601, which looks like 2013-01-08T13:00:00.000+11:00.

确实,当您 JSON.stringify 一个 javascript Date 对象时,它使用 ISO8601 格式.ko.mapping.toJSON 也会发生这种情况.

Indeed, when you JSON.stringify a javascript Date object, it uses the ISO8601 format. This also happens with ko.mapping.toJSON.

这个问题有几种解决方案,客户端和服务器端.这篇博文详细描述了这个问题,并提供了一些可能对您有所帮助的很好的答案.

There are several solutions to this problem, both client side and server side. This post describes the problem in detail and has some great answers that may help you.

恕我直言,最好的解决方案是让您的 MVC 控制器发出和使用 ISO8601,而不是旧的 Microsoft 日期格式.最简单的方法是使用 Json.Net 库 现在有 ISO8601 作为默认值 所以你甚至不需要自定义它.在客户端,您可能还想查看 Moment.js - 它可以轻松解析和格式化 ISO日期.

IMHO, the best solution is have your MVC controller emit and consume ISO8601 rather than the old Microsoft date format. The easiest way to do that is by using the Json.Net library which now has ISO8601 as the default so you don't even need to customize it. On the client side, you might also want to look at Moment.js - which makes it easy to parse and format ISO dates.

这篇关于淘汰赛日期在发布到 mvc 控制器时被重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆