简体   繁体   English

如何从具有Datetime属性的对象的IEnumerble列表中的Ajax调用正确序列化Datetime

[英]How can I correctly serialize Datetime from an Ajax call inside an IEnumerble list of objects that have a Datetime property

So I have a IEnumerable list of an object in my View as my model. 因此,在模型中,我的视图中有一个IEnumerable对象的列表。 The object has fields such as strings, ints, bools, and Datetime. 该对象具有字符串,整数,布尔值和日期时间等字段。 I want to serialize that list but the issue is that everything is done correctly except for the Datetime and for some reason it serializes into an incorrect number of ticks resulting in the wrong date when I print it out in my Controller. 我想序列化该列表,但问题是除日期时间外,其他所有步骤均正确完成,并且由于某种原因,序列化到不正确的滴答数,导致在控制器中打印出错误的日期。 So I have a variety of dates either June or July 2019 and they all become 01/00/1900. 所以我有多个日期为2019年6月或7月,它们都变为01/00/1900。 This is being used to export DataTable entries into an Excel sheet. 这用于将DataTable条目导出到Excel工作表中。

I have tried iterating through the IEnumerable list with several methods but none work in getting access to the datetime property properly so I cannot just make a second list of those date times. 我尝试用几种方法遍历IEnumerable列表,但是没有一种方法可以正确访问datetime属性,因此我不能仅列出这些日期时间的第二个列表。

$("#btnExport").on("click", function () {
      var modelData = '@Html.Raw(Json.Encode(Model))';
      $.ajax({
            type: "POST",
            url: '@Url.Action("ExportTransactionsInExcel", "Gatelog")',
            contentType: "application/json; charset=utf-8",
            data: modelData,
            success: function (result) {
            },
            error: function (obj) {
               alert("fail");
            }
      });
});

So some of the ticks are... Date(1561262400000) Date(1563854400000) Date(1561867200000) Date(1564459200000) I want to be able to encode the list and still have it return the correct dates 所以一些滴答是... Date(1561262400000)Date(1563854400000)Date(1561867200000)Date(1564459200000)我希望能够对列表进行编码,并且仍然让它返回正确的日期

So I think it has already been covered that javascript Date using milliseconds from the UTC epoch 1/1/1970 where C# uses 100 nanosecond ticks from 01/01/0001 . 因此,我认为从UTC时代1/1/1970起使用毫秒的javascript日期已经被涵盖,其中C#使用从01/01/0001 100纳秒刻度。 There are two solid options for dealing with this. 有两种可靠的解决方案。 The first would be to create your own JsonConverter and handle the logic there. 首先是创建您自己的JsonConverter并在那里处理逻辑。

The approach I've chosen to display is a bit simpler. 我选择显示的方法比较简单。 Instead of deserializing directly to a DateTime object, lets deserialize to a long variable instead. 与其直接反序列化为DateTime对象,不如反序列化为long变量。 Then, we can add a DateTime variable with a custom getter that will handle assignment for you. 然后,我们可以添加带有自定义getter的DateTime变量,该变量将为您处理分配。

public class Model
{
    public long time {get;set;}

    public DateTime datetime 
    {
       get
       {
           return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
              .AddMilliseconds(time)
              .ToLocalTime();
       }
    }   
 }

Just make sure that the figure coming in deserializes to the long value and then in your C# code only reference the DateTime value 只需确保输入的数字反序列化为long值,然后在您的C#代码中仅引用DateTime值即可

I ended up just making a new model and creating a JS object that matches those models fields exactly. 最后,我只是制作了一个新模型,并创建了一个与这些模型字段完全匹配的JS对象。 Then I passed the parameters into the new object and called ajax on it. 然后,我将参数传递到新对象中,并在其上调用ajax。 I re-ran the query from the controller which allowed me to still get the parameters I need and it would perform faster if the list got really large versus doing it all front end. 我从控制器重新运行了查询,这使我仍然可以获取所需的参数,并且如果列表真的很大而不是全部在前端,它将执行得更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM