I am trying to send array of ids to server side(controller) from MVC razor view but when number of ids is more than 2000 as query string length, I get the following error and process stops.
I tried to send data using POST method also but I got error. The last screenshot is the error I got using POST method.
I have pasted down here my server side and Jquery code.
JQUERY CODE
Process 1: Regular method
Process 2: Alternative method for sendig data using POST method
public void ExportPageStatusHistory(string[] PrescriptionIds)
{
ExcelPackage excel = new ExcelPackage();
var fileName = "PageStatusHistoryReport_" + DateTime.Now.ToStr();
var workSheet = excel.Workbook.Worksheets.Add(fileName);
var lst = string.Join(",", PrescriptionIds);
var result = PharmacyReferralService.GetPageASPNHistory(lst);
if (result.Count > 0)
{
workSheet.Cells[1, 1].LoadFromCollection(result, true);
workSheet.Cells.AutoFitColumns();
workSheet.Cells["A1:Z1"].Style.Font.Bold = true;
workSheet.Column(6).Width = 35;
workSheet.Column(7).Width = 35;
using (ExcelRange col = workSheet.Cells[2, 6, 1 + result.Count, 6])
{
col.Style.Numberformat.Format = "yyyy-MM-dd hh:mm AM/PM";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
using (ExcelRange col = workSheet.Cells[2, 7, 1 + result.Count, 7])
{
col.Style.Numberformat.Format = "yyyy-MM-dd hh:mm AM/PM";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
Jquery Code:
function ExportPrescriptionStatusHistory() {
$('#tbl_PrescriptionsDrugReport tbody tr td:nth-child(1)').each(function () {
items.push($(this).find("a").attr("data-id"));
});
@*var url = '@Url.Action("ExportPageStatusHistory", "Common")?PrescriptionIds=' + items;
location.href = url;*@
var url = '@Url.Action("ExportPageStatusHistory", "Common")';
$.ajax({
url: url,
type: "POST",
data: { PrescriptionIds: items },
async: false,
success: function(response){
},
error:function(xhr, ajaxOptions, thrownError)
{
alert(xhr.responseText);
ShowMessage("Error", "fail");
}
});
}