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 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");
}