-2

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.

enter image description here

I have pasted down here my server side and Jquery code.

Server Side code enter image description here

JQUERY CODE

Process 1: Regular method

enter image description here

Process 2: Alternative method for sendig data using POST method enter image description here

enter image description here

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");
                }
            });
        }
Samyukta R.
  • 154
  • 1
  • 1
  • 15
  • what does the decoded version of the error message say? – mahlatse Sep 26 '18 at 08:43
  • 1
    You need to include your code, not images of it. –  Sep 26 '18 at 08:49
  • @mahlatse: encrypted message can't be copied and decoded. – Samyukta R. Sep 26 '18 at 08:54
  • @SamyuktaR., if you can, try using a utility like fiddler to check the messages you send to the server, it will have a decode option for that error message – mahlatse Sep 26 '18 at 08:58
  • @StephenMuecke: I have pasted the original code. Please check. – Samyukta R. Sep 26 '18 at 08:59
  • You need to add the `traditional: true` ajax option (or you can add `contentType" 'application/json'` and use `data: JSON.stringfy({ PrescriptionIds: items }),` (but its unclear what the point of this is - you just seem to be posting back the data that the controller sent to the view - i.e. unchanged) –  Sep 26 '18 at 09:02
  • @StephenMuecke: I am actually trying to create Excel-sheet passing those ids from view to controller. – Samyukta R. Sep 26 '18 at 09:04
  • @StephenMuecke: What's the solution then, please suggest me. Any help would be grateful. – Samyukta R. Sep 26 '18 at 09:06
  • I already told you how to send the data correctly in my previous comment –  Sep 26 '18 at 09:07
  • But your need to make a GET if you are wanting to download a file - refer [Download Excel file via AJAX MVC](https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Sep 26 '18 at 09:09
  • @StephenMuecke: I tried with your suggested code but no luck. I get the same error of "Request Url too long". I tried with GET method also. – Samyukta R. Sep 26 '18 at 09:38
  • @StephenMuecke: Using GET method doesn't even hit the controller and shows an error of Request Url is too long. – Samyukta R. Sep 26 '18 at 09:39
  • Do you even read the link I gave you! –  Sep 26 '18 at 09:40
  • @StephenMuecke: Yes I did and followed the same pattern but no luck so far. – Samyukta R. Sep 26 '18 at 09:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180785/discussion-between-stephen-muecke-and-samyukta-r). –  Sep 26 '18 at 09:46

1 Answers1

0

you don't sent data in path url parameter. Please, sent it in request body. so, only idea. Because, the browser limited url length, maximun ~ 2000 byte.

for example .

Server code:

public void updateData([FromBody] string[] data) {
    //...
}

Client code

$.ajax({
    type: "POST",
    url: "/server",
    data: JSON.stringify(data),
    contentType: "application/json", 
    datatype: "html",
    success: function (data) {
        $('#result').html(data);
    }
});

I only JAVA, I think C# is the same. please, see more What is the maximum length of a URL in different browsers?

mahlatse
  • 1,322
  • 12
  • 24
VuNT
  • 21
  • 3