Click here to Skip to main content
12,501,479 members (52,276 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Ajax MVC jQuery MVC3 Razor
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!
$(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:
 $('.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)
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 19-Jul-12 20:34pm
Updated 19-Jul-12 20:50pm
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 20 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100