views:

76

answers:

1

I intended for this function to call my MVC action method that returns a CSV report.

$(function() {
    $('#exportButton').click(function() {
        $.get('/curReport/GetCSVReport');
    });
});

If I make a button like the code below then, when it is clicked, I'm given the "Open with/Save File" window.

<input type="button" value="Export" onClick="location.href='CurReport/GetCSVReport'">

However, when I change my button to use my jQuery function then, although GetCSVReport() is called I'm not given the "Open with/Save File" window.

Here is my GetCSVReport()

public FileResult GetCSVReport()
{
    ...
    return fileResult;
}

How can I get my jQuery function to work like the onClick?

Thank you,

Aaron

+5  A: 

Use the same code you used before:

$(function() {
    $('#exportButton').click(function() {
        location.href = 'CurReport/GetCSVReport';
    });
});

get triggers an Ajax call, which you do not want here. You use jQuery to bind the event, but the action stays the same.

To add query string parameters, use:

location.href = 'CurReport/GetCSVReport?filter=' + escape(val);
Kobi
How do I add values to the parameters? Using the old way I could have just done this "$.get('/curReport/GetCSVReport', {filter: val});". Thank you!
Aaron Salazar
@Aaron - updated the answer. If `val` has a simple value (eg, number), you don't have to escape it.
Kobi
Thank you for your help and the updated answer!!
Aaron Salazar