0

I have a view where I make take information from a row in Jqgrid and then make an ajax call to the controller using that info as parameteres. The parameters are being passed to the controller however when the it does not perform the action that I desire, I am trying to create a csv file.

View:

    function sendParams() {
    var selId = jQuery("#list").jqGrid('getGridParam','selrow');
    var data = jQuery("#list").jqGrid('getRowData',selId);

        $.ajax({
        type: "GET",
        url: '@Url.Action("ExportToExcel","Home")',
        data: {sidx: 'abcd', sord: 'desc', page:1, rows: 100, id: data.selId, rid: data.id, process: data.process, detail_table: data.detail_table, jobGroup: @Html.Raw(Json.Encode(@ViewBag.jobGroup)), date: @Html.Raw(Json.Encode(@ViewBag.DateSelected))},
        contentType: "application/json; charset=utf-8",

        });
}   


      <button onclick="function sendParams()">Export</button>

Controller

    public ContentResult ExportToExcel(String sidx, String sord, int page, int rows, String id, String process, String detail_table, String jobgroup, String date)
    {


            var sw = new StringWriter();

            List<myModel alist = DAL.ExportExcelConn2.SelectGridItems(sidx, sord, page, rows, rid, process, detail_table, jobgroup, date);

            foreach (var item in alist)
            {

                sw.WriteLine(String.Format("{0}, {1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16}", item.abc, item.sstuff, item.stuff2, item.stuff3, item.stuff4, item.stuff5, item.stuff6, item.stuff7, item.stuff8, item.stuff9, item.stuff10, item.stuff11, item.stuff12, item.stuff13, item.stuff14, item.stuff15, item.stuff16));

            }


            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=Exportabc" + DateTime.Now + ".csv");
            Response.ContentType = "text/csv";
            Response.Write(sw);
            Response.End();


        return Content("Test");


    }
CharlesMighty
  • 572
  • 1
  • 5
  • 15

1 Answers1

0

If you use $.ajax call you can process the server response inside of success or error callback. What you really need in case of returning CSV or Excel data is setting of new window.location.

For example

 window.location = '@Url.Action("ExportToExcel","Home")' + '?' +
    $.param({
        sidx: 'abcd',
        sord: 'desc',
        page:1,
        rows: 100,
        id: data.selId,
        ... // -- all other options
    });

In the case web browser will automatically open the program on the computer (like Excel).

I recommend you additionally to read the answer which contain ASP.NET MVC Project which export data to Excel (real XLSX file).

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • will this be the actual code? it doesn't work, please let me know if it is a syntax error? – CharlesMighty Dec 18 '12 at 18:56
  • I personally use another structure in my project to build URLs. So I not sure what you mean under "be the actual code"? To fix previously posted code one should be just add `'?'` between `ExportToExcel` and parameters. – Oleg Dec 18 '12 at 19:15
  • Thank you so much, your solution worked, i have been breaking my head since yesterday. Appreciate your help. I have been following your posts for a while for jqGrid, seriously dude you should write a book I think you are really knowledgeable. Thanks once again :) – CharlesMighty Dec 18 '12 at 20:22
  • @CharlesMighty: You are welcome! I though already about writing of the book, but then I get project with a lot of work for many moths. So I had only some time to help other on stackoverflow. Probably in February I will get more time and will come back to the book. – Oleg Dec 18 '12 at 21:43