Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a strongly-typed partial view containing a table, whose entire data i want to pass as a parameter in a controller method which wud export the data to a .xls file. Now how can i serialize the data using jQuery/ajax to pass the table data to the ActionMethod. Putting the table inside a 'form' element doesn't load the partial view at all!
This is the jQuery function i was trying to use. But it doesnt execute after
alert(controllerPath); I am not quite sure what to do!
JavaScript
$(function () {
    $('#btnExcel').click(function () {

        //var params = { dclist: $('div.shareDiv').serializeAnything() }
        var controllerPath = '@Url.Content("~/Declaration")';
        
        alert(controllerPath);
        $.ajax({
            type: 'post',
            dataType: 'html',
            url: controllerPath + "/ExportToXLS?dclist=" + $('div.shareDiv').serializeAnything(),
            async: true
        });
        alert(url);
    });
});
    
[N.B. serializeAnything is a jQuery Extension that Serializes anything (and not just forms!)]


This function loads the partial view:
JavaScript
 $('.btn-show').click(function (e) {
        e.preventDefault();
        var href = summaryPath;
        href += "?date=" + encodeURIComponent($('#dcDate').val());
        $('div.shareDiv').load(href, function (response, status, xhr) {
            $('div.shareDiv').unblock();
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                $('.shareDiv').html(msg + xhr.status + "<br/>" + xhr.statusText + "<br/>" + xhr.responseText);
            }
            else {
                if ($('#shareTable')) {
                    var newSt = new superTable("shareTable", {
                        fixedCols: 2,
                        headerRows: 1
                    });
                }
            }
        });

    });

});



And this is the Action Method to which the serialized data is to be passed(in case needed)
C#
public ActionResult ExportToXLS(IList<Lib.Models.Declaration> dclist)
       {
           DeclarationService service = new DeclarationService();
           Response.AddHeader("content-disposition", "attachment; filename=" + "DCSummary" + DateTime.Now.Date.ToString("{ddMMyyyy hh-mm-ss tt}") + ".xls");
           byte[] file = DeclarationExport.CreateExcelFromModelList(dclist.ToList());
           return File(file, "application/excel");
       }
Posted
Updated 19-Jul-12 20:50pm
v3

1 solution

Oh, I see. You have arbitrary data you want to pass with AJAX ? I think the issue is that AJAX is not the right way to do a download. If you do a full post to a URL that returns a file, your page won't refresh, it will download the file. An AJAX call will return data to your javascript, which can't be turned in to a download, because it's not a full page request.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900