1

On my view I have a button for export in CSV the current data view. This is a simple link to a method in the controller.

<input type="button" value="Exporter" id="btnexport" class="btnsearch" onclick="location.href='@Url.Action("ExportCSV", "AuditAddins")'" />

And the controller method

public void ExportCSV()
{
    auditAddins = AddinsCache.AuditAddinsCache;
    var sw = new StringWriter();
        sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", "Date", "AddinName", "IsAddinActive", "UserName", "PcName"));
        foreach (var record in auditAddins)
        {
            sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", record.Date, record.AddinList.AddinName, record.IsAddinActive, record.Users.UserName, record.Pc.PcName));
        }
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=Export.csv");
        Response.ContentType = "text/csv";
        Response.Write(sw);
        Response.End();
}

It's working well. But I need to send a parameter to this method, because I have three design mode, and each have their own column order. So I have created a jquery/ajax script to send the paramater.

$("#btnexport").click(function () {
    var index = $('#CritID').find('option:selected').val();
    var d = { chx: index}
    $.ajax({
        url: "/AuditAddins/ExportCSV",
        type: "POST",
        data: d,
        success: function (data) {
            alert("Export ok !");
        },
    });
});

The main objective is to change the export method like that :

 public void ExportCSV(int chx)
    {
        ...
        switch (chx)
        {
            case 1:
                sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", "Date", "AddinName", "IsAddinActive", "UserName", "PcName"));
                break;
            case 2:
                sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", "Date", "UserName", "PcName","AddinName", "IsAddinActive"));
                break;
            ...
        }

The problem is that I don't have IE download window anymore. I have tried with actionresult method, it's doesn't work either.

mrplume
  • 183
  • 1
  • 3
  • 18

1 Answers1

0

You don't have to use POST to send a parameter.

Instead, you can add a parameter to the GET request:

/AuditAddins/ExportCSV?index=2

and default to 1 if no parameter is provided.

To send multiple parameters, you can use & to separate them:

/AuditAddins/ExportCSV?index=2&param2=123

This should also solve the problem with the download window.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • I have tried with : var url = "/AuditAddins/ExportCSV?chx=1"; $.get(url, null, function (data) { alert("done"); }); But it's not working. – mrplume Feb 10 '16 at 15:46
  • @mrplume - Did you process/handle that parameter on the server side too? – Danny_ds Feb 10 '16 at 15:51
  • Yes. I have breakpoints, and I see that the paramater works, and the csv creation too. But I don't understand why IE little download popup doesn't show up. – mrplume Feb 10 '16 at 15:57
  • @mrplume - That's strange - it should work exactly the same as without the parameter. Does the csv get displayed in the browser instead? – Danny_ds Feb 10 '16 at 16:05
  • No... nothing happens. I'm using a .done(function (result) {alert("done");}); does I need to change alert("done") ? I'm really need Jquery to get the value of mode dropdownlist (this is the parameter). – mrplume Feb 10 '16 at 16:35