ajax post空数组,arrays - AJAX Post大型JSON对象数组返回NULL - 堆栈内存溢出

博主正试图从JSON对象数组创建Excel文件。在控制器中,他们使用AJAX发送数据并尝试将数据转换为Excel,但遇到了问题。列表`ClientBookingInfo e`在控制器中返回NULL。可能的原因包括JSON编码错误或数据类型不匹配。博客中提到了`ClientBookingInfo`类的定义,并展示了如何构建和发送JSON数据的代码片段。
摘要由CSDN通过智能技术生成

我正在尝试从JSON对象数组在控制器上生成Excel文件。 我只传递了一个JSON对象,而我使用的相同方法似乎不适用于List或JSON数组。

AJAX生成对象:

$.ajax({

type: "GET",

url: '/home/GetInfoToExportCalgary',

success: function (data) {

//console.log(data);

CalgaryBookingInfo = data;

for (var i = 0; i < CalgaryBookingInfo.length; i++) {

if (CalgaryBookingInfo[i].length != 0) {

//console.log(CalgaryBookingInfo[i][0].Company + " has bookings")

var SumTime = 0;

for (var j = 0; j < CalgaryBookingInfo[i].length; j++)

{

CalgaryNotNullBookings.push({

Company: CalgaryBookingInfo[i][j].Company,

Location: CalgaryBookingInfo[i][j].Location,

PaidHours: CalgaryBookingInfo[i][j].HoursPaid,

Start: CalgaryBookingInfo[i][j].Start,

End: CalgaryBookingInfo[i][j].End,

Time: CalgaryBookingInfo[i][j].Time,

Total: SumTime + CalgaryBookingInfo[i][j].Time

});

SumTime += CalgaryBookingInfo[i][j].Time;

}

CalgaryNotNullBookings.push({

Company: "",

Location: "",

PaidHours: "",

Start: "",

End: "",

Time: "",

Total: SumTime

});

}

}

console.log(CalgaryNotNullBookings);

//buildHtmlTable(RetreivedClients);

}

})

AJAX发送数组:

在这里,我只是发送一个POST,其中包含我预先生成的数据并单击一次按钮即可调用。

function ExportCalgary(data) {

$.ajax({

type: "POST",

url: '/home/ExportCalgary',

data: data,

success: function (data) {

console.log("huehuehuehuehueh");

if (data.status) {

//Refresh the calender

//location.reload();

$('#myModalSave').modal('hide');

}

},

error: function () {

alert('Failed');

}

})

}

控制器开机自检功能:

[HttpPost]

public JsonResult ExportCalgary(List e)

{

var status = false;

//string[] columns = { "Company", "Location", "PaidHours", "Start", "End", "Time" , "Total" };

//byte[] filecontent = ExcelExportHelper.ExportExcel(dataToConvert, "Technology", true, columns);

//return File(filecontent, ExcelExportHelper.ExcelContentType, "CalgaryClientInfo_" + DateTime.Now.Year+"_" + DateTime.Now.Month + ".xlsx");

ExcelPackage excel = new ExcelPackage();

var workSheet = excel.Workbook.Worksheets.Add("Sheet1");

workSheet.TabColor = System.Drawing.Color.Black;

workSheet.DefaultRowHeight = 12;

//Header of table

//

workSheet.Row(1).Height = 20;

workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

workSheet.Row(1).Style.Font.Bold = true;

workSheet.Cells[1, 1].Value = "Company";

workSheet.Cells[1, 2].Value = "Location";

workSheet.Cells[1, 3].Value = "PaidHours";

workSheet.Cells[1, 4].Value = "Start";

workSheet.Cells[1, 5].Value = "End";

workSheet.Cells[1, 6].Value = "Time";

workSheet.Cells[1, 7].Value = "Total";

//Body of table

//

int recordIndex = 2;

for(int i = 0; i < e.Count; i++)

{

workSheet.Cells[recordIndex, 1].Value = e[i].Company;

workSheet.Cells[recordIndex, 2].Value = e[i].Location;

workSheet.Cells[recordIndex, 3].Value = e[i].PaidHours;

workSheet.Cells[recordIndex, 4].Value = e[i].Start;

workSheet.Cells[recordIndex, 5].Value = e[i].End;

workSheet.Cells[recordIndex, 6].Value = e[i].Time;

workSheet.Cells[recordIndex, 7].Value = e[i].Total;

recordIndex++;

}

workSheet.Column(1).AutoFit();

workSheet.Column(2).AutoFit();

workSheet.Column(3).AutoFit();

workSheet.Column(4).AutoFit();

workSheet.Column(5).AutoFit();

workSheet.Column(6).AutoFit();

workSheet.Column(7).AutoFit();

string excelName = "CalgaryClientInfo_" + DateTime.Now.Year+"_" + DateTime.Now.Month;

using (var memoryStream = new MemoryStream())

{

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Response.AddHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx");

excel.SaveAs(memoryStream);

memoryStream.WriteTo(Response.OutputStream);

Response.Flush();

Response.End();

status = true;

}

return new JsonResult { Data = new { status = status } };

}

当前,当我调试时,列表“ ClientBookingInfo e”返回NULL,我不确定是怎么回事。 可能与JSON的编码或数据类型有关吗? 我是否缺少一些行,这些行指定要发送的JSON将是数组?

这也是类ClientBookingInfo:

public class ClientBookingInfo

{

public string Company { get; set; }

public string Location { get; set; }

public int PaidHours { get; set; }

public DateTime Start { get; set; }

public DateTime End { get; set; }

public int Time { get; set; }

public double Total { get; set; }

}

提前致谢! :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值