views:

28

answers:

1

Hi!

I've created a custom Action Result in my MVC application, where I generate a CSV file according to the configuration that the user provides.

However. The problem I have now is that if I create a download link like this:

<a ID="lnkDownload" href="<%= Url.Action("DownloadFile", "Download", new { id = Model.Id, userId = Model.userId, startDate = "30/05/2010", endDate = "30/05/2005"}) %>">Download</a>

it all works fine. The thing is that I've now introduced two date time controllers on the page, so that the user can set the start and end date. Therefore I'm now adding in the dates from the controller to the url like this:

$('#lnkDownload').click(function() {
    var startDate = $('#tbExtrateStartDate').val();
    var endDate = $('#tbExtrateEndDate').val();

    var link = $('#lnkDownload').attr('href') + "&startDate=" + startDate + "&endDate=" + endDate;

    $.get(link, function() {

    });
    return false;
});

This gives me the problem that the "download prompt" does not appear at all. I've tried to figure out why, but haven't really found any solution.

How can I solve this?

+2  A: 

The reason for the download not working is because you are attaching a click handler to the anchor and then send an AJAX request $.get to the action instead of a normal request which won't work.

You can keep the click handler but don't send AJAX request, simply update the href of the anchor.

Darin Dimitrov
Cool. Worked out pretty well. Done so many requests now through jquery, so almost forgot I don't have to do it all the times.
MrW