2016-12-25 56 views
0

我在c#中开发了一个MVC web应用程序,并且使用Typescript前端。 我有一个方法进入控制器,接收带有数据模型的HttpPost请求,这个数据模型是用typelite自动生成到打字稿类中的。使用ajax发送打印稿日期

在我的请求数据模型中有一个日期时间字段,当我发送请求到后端时,数据时间字段被序列化为以下格式的字符串:“Sun + Dec + 25 + 2016 + 11:29:33 + GMT + 0100 +(ora + solare + Europa + occidentale)“ 我喜欢这个文件被serilize到UTC datetime字符串。

我的打字稿代码发送请求:

$.ajax({ 
     method: callingMethod, 
     url: urlToCall, 
     data: *dataValue, 
     beforeSend: function() { 
      self.BeforeAsyncAction(); 
     }, 
    }) 
    .done(callbackDone) 
    .fail(callbackFail) 
    .always(self.CompleteAsyncAction); 
} 

dataValue与此接口的类:我必须序列化到UTC日期时间

export class FileServiceModel extends Gedoc.WebApplication.ServiceModels.BaseServiceModel { 
    Allegato: Gedoc.WebApplication.ServiceModels.FileStreamServiceModel; 
    Attributi: Gedoc.WebApplication.ServiceModels.AttributoServiceModel[]; 
    Descrizione: string; 
    DimensioneByte: number; 
    *DtIn: Date; 
    *DtRegistrazione: Date; 
    *DtUp: Date; 
    Id: number; 
    Tags: string; 
    Titolo: string; 
} 
  • 领域。

如何最好的方式来序列自动这一领域

感谢认为

回答

0

JavaScript的Date对象有一个toUTCString方法,所以:

let d = new Date(); 
console.log(d); // Sun Dec 25 2016 13:36:02 GMT+0200 (IST) 
console.log(d.toUTCString()); // Sun, 25 Dec 2016 11:36:02 GMT 

在你的情况,你可以做一些事情如:

function normalizeDate(data: FileServiceModel) { 
    let copy = Object.assign({}, data); 
    copy.DtIn = data.DtIn.toUTCString(); 
    copy.DtRegistrazione = data.DtRegistrazione.toUTCString(); 
    copy.DtUp = data.DtUp.toUTCString(); 

    return copy; 
} 

$.ajax({ 
     method: callingMethod, 
     url: urlToCall, 
     data: normalizeDate(dataValue), 
     beforeSend: function() { 
      self.BeforeAsyncAction(); 
     }, 
    }) 
     .done(callbackDone) 
     .fail(callbackFail) 
     .always(self.CompleteAsyncAction); 
}