Click here to Skip to main content
15,894,405 members

How to serialize data in a table and pass it as parameter to controller method in MVC using jquery/ajax?

pratip_gd asked:

Open original thread
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");
       }
Tags: Ajax, MVC (MVC3), jQuery, Razor

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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