Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于JSON转换,数据库中存在.NET日期格式_C#_.net_Json_Asp.net Mvc_Json.net - Fatal编程技术网

C# 由于JSON转换,数据库中存在.NET日期格式

C# 由于JSON转换,数据库中存在.NET日期格式,c#,.net,json,asp.net-mvc,json.net,C#,.net,Json,Asp.net Mvc,Json.net,我正在尝试获取与数据库相同的日期格式,如12/2/2020,但json正在转换日期,如/date(160684560000)/ 我想保持数据库中的日期不变。我只需要日期而不是时间 任何帮助都将不胜感激 谢谢 这是我正在使用的模型 public Item() { this.Inventories = new HashSet<Inventory>(); this.PurchasesDetails = new HashSet

我正在尝试获取与数据库相同的日期格式,如
12/2/2020
,但json正在转换日期,如
/date(160684560000)/

我想保持数据库中的日期不变。我只需要日期而不是时间

任何帮助都将不胜感激

谢谢

这是我正在使用的模型

 public Item()
        {
            this.Inventories = new HashSet<Inventory>();
            this.PurchasesDetails = new HashSet<PurchasesDetail>();
            this.SalesDetails = new HashSet<SalesDetail>();
        }

        public int Id { get; set; }
        public string Code { get; set; }
        public int CategoryID { get; set; }
        public string Name { get; set; }
        public int MeasurementID { get; set; }
        public int Quantity { get; set; }
        public decimal BuyPrice { get; set; }
        public decimal SalePrice { get; set; }
        public decimal CommisionRate { get; set; }
        public Nullable<System.DateTime> MftDate { get; set; }
        public Nullable<System.DateTime> ExpDate { get; set; }
        public Nullable<int> StockLimit { get; set; }
        public string Description { get; set; }
        public string UserID { get; set; }

        public virtual AspNetUser AspNetUser { get; set; }
        public virtual Category Category { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Inventory> Inventories { get; set; }
        public virtual Measurement Measurement { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<PurchasesDetail> PurchasesDetails { get; set; }
       // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SalesDetail> SalesDetails { get; set; }
    }
var itemz = new
        {
            item.Id,
           item.Code,
            item.Name,
            item.Quantity,
            item.BuyPrice,
            item.SalePrice,
            item.CommisionRate,
            item.CategoryID,
            item.UserID,
            item.MeasurementID,
            item.MftDate,
            item.ExpDate,
            item.StockLimit,
            item.Description
        };
        return Json(new { data = itemz }, JsonRequestBehavior.AllowGet);
这是输出

{
"data": {
"Id": 10,
"Code": "33",
"Name": "dd",
"Quantity": 44,
"BuyPrice": 45,
"SalePrice": 50,
"CommisionRate": 0,
"CategoryID": 6,
"UserID": "57e8c446-ccb7-4399-a7f3-898ccc8066fd",
"MeasurementID": 1,
"MftDate": "/Date(1606845600000)/",
"ExpDate": "/Date(1579543200000)/",
"StockLimit": 7,
"Description": null
}
}

您正在itemz中定义匿名类型。因此,你正在管理它。如果希望在json结果中保留部分日期时间。例如,您只需要指定为所需格式的字符串

var itemz = new
    {
        item.Id,
       item.Code,
        item.Name,
        item.Quantity,
        item.BuyPrice,
        item.SalePrice,
        item.CommisionRate,
        item.CategoryID,
        item.UserID,
        item.MeasurementID,
        item.MftDate.ToString("dd/MM HH:mm:ss"),
        item.ExpDate.ToString("dd/MM/yyyy"),
        item.StockLimit,
        item.Description
    };
    return Json(new { data = itemz }, JsonRequestBehavior.AllowGet);

这里有更多关于方法的信息。

您应该
将日期转换为正确的格式

因此,您应该像下面这样更改代码

var itemz = new
{
    item.Id,
    item.Code,
    item.Name,
    item.Quantity,
    item.BuyPrice,
    item.SalePrice,
    item.CommisionRate,
    item.CategoryID,
    item.UserID,
    item.MeasurementID,
    MftDate = item.MftDate == DateTime.MinValue ? "" : item.MftDate.Value.ToString("dd/MM/yyyy"),

    ExpDate = item.ExpDate == DateTime.MinValue ? "" : item.ExpDate.Value.ToString("dd/MM/yyyy"),
    item.StockLimit,
    item.Description
};

mftDate的值是如何设置的?你能提供物品类别吗?你在存储时间和日期吗?对不起。我已经编辑并添加了item类。回答很好,Antonio:)这就是模型中的“public Nullable MftDate{get;set;}public Nullable ExpDate{get;set;}”的方式,所以它说anyonmous类型成员声明符无效&方法“ToString”没有重载在添加ToString(“dd/MM/yyyy”)时接受1个参数错误,好吗。因此,两个属性的类型都可以为null。在这种情况下,您必须检查日期是否有值,并像Anonymous那样为属性定义名称。e、 g:var itemz=new{item.Id,item.Code,item.Name,MftDate=item.MftDate.HasValue?item.MftDate.Value.ToString(“dd/MM HH:MM:ss”):string.Empty};返回Json(新的{data=itemz},JsonRequestBehavior.AllowGet);所以它表示无效的anyonmous类型成员声明符&方法“ToString”没有重载,在添加ToString(“dd/MM/yyyy”)时出现1个参数错误,我尝试了“DateTime”?MftD=item.MftDate;MftD.Value.ToString(“dd/MM/yyyy”);约会时间?ExpD=item.ExpDate;ExpD.Value.ToString(“dd/MM/yyyy”);var itemz=new{item.MftD,item.ExpD,};`但是没有luckCan你能给我你的
item.MftDate,item.ExpDate,
类的类型吗?是
DateTime
type吗?如何在var itemz=new{}中添加它们?我收到了这个错误。请看一看。