Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
6
Reply
Export data as PDF Using JQuery In ASP.NET MVC
Learn Avid
Nov 27 2017 4:07 PM
348
Reply
Hello,
I'm actually following http://www.c-sharpcorner.com/article/basic-pdf-export-using-jquery-in-asp-net-mvc-razor/ article to bind data from controllers action to HTML table and then export data from HTML table to iTextSharp pdf viewer. Below i'm binding controllers action method data to html table.
@model List
<
ExportDataAsPDFAppl.Controllers.Employees
>
@using (Html.BeginForm("exportPDF", "Home", FormMethod.Post))
{
<
div
>
<
input
type
=
"hidden"
name
=
"gridHTML"
/>
<
input
type
=
"submit"
id
=
"btnExportHTML"
name
=
"btnExportHTML"
class
=
"btn btn-success"
value
=
"Export Pdf"
/>
</
div
>
<
div
id
=
"tblEmpData"
>
<
table
class
=
"table table-striped table-condensed"
>
<
thead
>
<
tr
>
<
th
>
ID
</
th
>
<
th
>
FirstName
</
th
>
<
th
>
LastName
</
th
>
<
th
>
Gender
</
th
>
<
th
>
Salary
</
th
>
</
tr
>
</
thead
>
<
tbody
>
@foreach (var item in Model)
{
<
tr
>
<
td
>
@item.ID
</
td
>
<
td
>
@item.FirstName
</
td
>
<
td
>
@item.LastName
</
td
>
<
td
>
@item.Gender
</
td
>
<
td
>
@item.Salary
</
td
>
</
tr
>
}
</
tbody
>
</
table
>
</
div
>
}
In btnExportHTML jQuery click function, hidden variable gridHTML is binded with HTML table.
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$(
'#btnExportHTML'
).click(
function
() {
debugger
;
$(
"input[name='gridHTML']"
).val($(
"#tblEmpData"
).html());
});
});
</script>
But on export 'btnExportHTML' click,
HTTP 404 error is raised.
Below is actionMethod for button click.
[HttpPost]
[ValidateInput(
false
)]
private
FileResult exportPDF(
string
gridHTML)
{
StringReader reader =
null
;
Document pdfDoc =
null
;
PdfWriter write =
null
;
try
{
using
(MemoryStream memorystream =
new
MemoryStream())
{
reader =
new
StringReader(gridHTML);
pdfDoc =
new
Document(PageSize.A4, 5f, 5f, 5f, 5f);
write = PdfWriter.GetInstance(pdfDoc, memorystream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(write, pdfDoc, reader);
pdfDoc.Close();
return
File(memorystream.ToArray(),
"application/pdf"
, Convert.ToString(DateTime.Now +
".pdf"
));
}
}
finally
{
reader.Dispose();
pdfDoc.Dispose();
write.Dispose();
}
}
please let me know my mistake to solve this issue.
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
6
)
Next Recommended Forum
SSRS 2008 issue in ASP.net web application
Remove white spaces from detail section of crystal report