将旋转ViewAsPdf与MVC 5

时间:2016-03-12 06:58:41

标签: c# asp.net-mvc asp.net-mvc-4

我使用Rotative ViewAsPdf使用以下代码将视图转换为pdf,项目需要登录,所有控制器都有[SessionExpire]。

我使用ViewBag将SchoolCode传递给视图以显示所选择的学校详细信息,视图工作正常但在调用下面的Action时,pdf文件显示没有任何数据的表格!

知道为什么吗? 有没有其他方法来保存视图与查询字符串和登录需要作为pdf?

public ActionResult DownloadViewPDF()
{
    ViewBag.SChoolCode = Request.QueryString["sid"];
    var a = new ViewAsPdf();
    a.ViewName = "SchoolDetails";
    var pdfBytes = a.BuildPdf(ControllerContext);
    MemoryStream ms = new MemoryStream(pdfBytes);
    return new FileStreamResult(ms, "application/pdf");
}

SchoolDetails视图使用以下ajax

        $.ajax({
            url: '@Url.Action("GetSchoolSearch")',
            type: 'POST',
            datatype: 'json',
            async: false,
            data: {
                p_SchoolCode: v_SchoolID,
            },
            success: function (result) {
                if (result.GetSchoolSearch != null) {
                    ko.cleanNode(document.getElementById('tblSInfo'));
                    ko.applyBindings(result, document.getElementById('tblSInfo'));
                    ResultArray = result.GetSchoolSearch;
                    jQuery.each(ResultArray, function (i, val) {
                        v_SchoolDesc = val["SCHOOL_DESC_AR"];
                        $("#sDesc").html(v_SchoolDesc);
                    })
                }
                else {
                    alert("Not Found!")
                    self.close();
                }
            }
        });

这是视图的输出

enter image description here

但是pdf是空白的:

enter image description here

1 个答案:

答案 0 :(得分:0)

不要让它太复杂。

        [HttpGet]
        public ActionResult GetContractsDetailsForPdfCreation(int contentId)
        {
            ContractDetailsModel contractDetails = new ContractDetailsModel();

            try
            {
                contractDetails = (ContractDetailsModel)contractBLObj.GetContractsDetailsForPdfCreation(contentId);
            }
            catch (Exception ex)
            {

            }
            return new ViewAsPdf("ContractPDFDownload", contractDetails) 
            {
                FileName = "ContractAgreement-"+contentId+".pdf"
            };

        }

此处ViewName为" ContractPDFDownload"和合同细节是模型