Home Export data as PDF Using JQuery In ASP.NET MVC
Reply: 0

Export data as PDF Using JQuery In ASP.NET MVC

Learn Avid
1#
Learn Avid Published in 2017-12-01 23:52:23Z

I'm following C# corner article to bind data from controllers action to HTML table and then export data from HTML table to iTextSharp pdf. 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 action Method for button btnExportHTML 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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.467682 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO