mvcrazortopdf首次单击ASP.NEt(会话未正确检索)

时间:2017-05-13 01:28:29

标签: c# asp.net mvcrazortopdf

我正致力于使用mvcrazortopdf下载pdf文件的程序。在第一次单击时,它会引发NullReferenceException错误。这是我的代码:

控制器:(ReviewerController.cs)

    public ActionResult PicOrderPDF() //error on this line
    {
        PurchaseOrder req = (PurchaseOrder)Session["purchaseOrderData"];
        var date = String.Format("{0:yyyyMMdd}", DateTime.Now);

        return new PdfActionResult(req)
        {
            FileDownloadName = date + "-PurchaseOrder" + (req.PicID).ToString("D4") + ".pdf"
        };

    }
    [HttpPost]
    public ActionResult DownloadPdf(PurchaseOrder purchaseOrder)
    {
        Session["purchaseOrderData"] = purchaseOrder;
        return RedirectToAction("PicOrderPDF", "Reviewer");

    }

型号:( PurchaseOrder.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SampleProject.Models
{
    public class PurchaseOrder : Account
    {

       public int PicID { get; set; }
       public DateTime RequiredDate { get; set; }

    }
}

PicOrderPDF是要下载的pdf的视图。我尝试在错误行添加一个断点来检查,结果是req为null。但是在继续并再次单击下载之后,它下载得很好。对不起,我是新来的

1 个答案:

答案 0 :(得分:1)

问题是第一次清除会话时通过添加

解决了
protected void Session_Start()
 {

 }

Global.asax.cs中,您还可以查看此session variables lost between controllers action methods