Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
XML
my partialview code:

Note: My partialview is loaded in pop up(jquery dialog box) and

I have 2 html buttons on click of them am callign the actionmethod which would to write the grid data to pdf/excel

@using System.Dynamic
@model List<System.Collections.IDictionary>
@{
Layout = null;
}
@{
var result = new List<dynamic>();

foreach (var emprow in Model)
{
var row = (IDictionary<string, object>)new ExpandoObject();
Dictionary<string, object> eachEmpRow = (Dictionary<string, object>)emprow;

foreach (KeyValuePair<string, object> keyValuePair in eachEmpRow)
{
row.Add(keyValuePair);
}
result.Add(row);
}
var gridReport = new WebGrid(result, canSort: true, ajaxUpdateContainerId: "gridReport",
rowsPerPage: 5);
gridReport.Pager(WebGridPagerModes.NextPrevious); ;
}
<script type="text/javascript">

function Print() {
//debugger;
$.ajax({
type: "POST",
url: '@Url.Action("GetPdf")',
data: {
strEMPID: $("#hdnEMPID").val(),
//presetName: $('#listboxStyle option:selected')
strpresetID: $('#lstFLDPresets option:selected').val()

},

error: function (ex) {
}
});

}
function GetExcel() {
//debugger;
$.ajax({
type: "POST",
url: '@Url.Action("GetExcel")',
data: {
strEMPID: $("#hdnEMPID").val(),
//presetName: $('#listboxStyle option:selected')
strpresetID: $('#lstFLDPresets option:selected').val()

},

error: function (ex) {
}
});

}
</script>
<div id="gridReport">
@if (@Model != null)
{
@gridReport.GetHtml(tableStyle: "webGrid", headerStyle: "head", alternatingRowStyle: "alt");
}
</div>

<div>
@* Dowload to Excel : @Html.ActionLink("Get Excel in Download Folder", "GetExcel")*@
<input id="btnExcel" type="button" value=" Export to Excel " onclick="GetExcel(); return false" />
</div>
<div>
@* Download as PDF : @Html.ActionLink("Open in PDF ", "GetPdf")*@
<input id="btnPdf" type="button" value="Export to PDF" onclick="Print(); return false" />
</div>
<div id="content" style="display: none; background: #c9dbff; align-content:center">
</div>

My Controller Action method

public void GetPdf(string strEMPID, int strpresetID)
{
try
{

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);
string griddata = wd.GetHtml().ToString();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=report_grid.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter s_w = new StringWriter();
HtmlTextWriter h_w = new HtmlTextWriter(s_w);
StringReader sr = new StringReader(griddata);
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


}
catch (Exception ex)
{
logger.Error(ex);
}
}

public void GetExcel(string strEMPID, int strpresetID)
{
try
{

//var result = fldHomeDAL.GetReports(strEMPID, strpresetID);

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);

string griddata = wd.GetHtml().ToString();

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);

string griddata = wd.GetHtml().ToString();
//string griddata = fldHomeDAL.GetReports(strEMPID, strpresetID).ToString();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=FLDReports.xls");
Response.ContentType = "application/excel";
Response.Write(griddata);
Response.End();
}
catch (Exception ex)
{
logger.Error(ex);
}

}

I have done narrow debugging, everything evrything is working fine but pdf /excel is not getting showed.

Any help would be greatly appreciated!!  TIA.
Posted

1 solution

just adding a small
success: function (result) {
$('#dialog-edit').html(result);
}
in my jquery function has solved the issue !!!
 
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