Click here to Skip to main content
11,790,613 members (62,839 online)
Rate this: bad
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")';
            type: 'post',
            dataType: 'html',
            url: controllerPath + "/ExportToXLS?dclist=" + $('div.shareDiv').serializeAnything(),
            async: true
[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) {
        var href = summaryPath;
        href += "?date=" + encodeURIComponent($('#dcDate').val());
        $('div.shareDiv').load(href, function (response, status, xhr) {
            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
Edited 19-Jul-12 20:50pm

1 solution

Rate this: bad
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.

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

  Print Answers RSS
0 OriginalGriff 1,114
1 Maciej Los 825
2 KrunalRohit 686
3 CPallini 611
4 ppolymorphe 610

Advertise | Privacy | Mobile
Web04 | 2.8.1509028.1 | Last Updated 20 Jul 2012
Copyright © CodeProject, 1999-2015
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