0

I need to generate a pdf copy of each partner's AR and for that I'm trying to pass a model to another view via TempData but it always returns null.

Here's my code below.

public ActionResult GenerateARPDFs(string dateasof)
{
    dateasof = "03-31-17";
    var report = new ReportViewModel();
    report.AccountsReceivable = _bhelper.GetAccountsReceivable("", "", "", DateTime.Parse(dateasof)).
                                Where(w => !w.SOANum.Any(y => char.IsLetter(y)) ||
                                w.SOANum.Contains("SOA")).ToList<AccountsReceivable>();

    foreach (var partner in report.AccountsReceivable.Select(m => m.BP).Distinct())
    {
        TempData["MyModel"] = report;
        var actionResult = new ActionAsPdf("AccountsReceivableReport_PerPartner", new { employeecode = partner })
        {   
            PageSize = Rotativa.Options.Size.Letter,
            PageOrientation = Rotativa.Options.Orientation.Landscape,
            PageMargins = new Rotativa.Options.Margins(5, 5, 5, 5),
            MinimumFontSize = 12
        };
        var byteArray = actionResult.BuildPdf(ControllerContext);
        var fullPath = ConfigurationManager.AppSettings["ArPDF"].ToString() + @"\" + partner + ".pdf";
        var fileStream = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite);
        fileStream.Write(byteArray, 0, byteArray.Length);
        fileStream.Close();
    }
    TempData["SuccessMessage"] = "Generation successful!";
    return View();
}



public ActionResult AccountsReceivableReport_PerPartner(string employeecode)
{
    var report = (ReportViewModel)TempData["MyModel"];
    report.AccountsReceivable = filter by partner here////....;
    return View(report);
}

But when it reaches the AccountsReceivableReport, the tempdata is always null. I can just recall the SP on the AccountsReceivableReport() but that would take longer.

Is there an issue with passing a tempdata to an ActionAsPdf? I'm using rotativa btw.

When I inserted a breakpoint, the TempData["MyModel"] is successfully filled with the results but when i reach the method for AccountsReceivableReport_PerPartner() it is now null.

If i try to change it from ActionAsPdf to ViewAsPdf it returns an error on the buildPdf part

beejm
  • 2,381
  • 1
  • 10
  • 19

1 Answers1

0

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary. TempData stays for a subsequent HTTP Request as opposed to other options (ViewBag and ViewData) those stay only for current request. detail

Try session for this:

public ActionResult AccountsReceivableReport_PerPartner(string employeecode)
{
    var report = Session["MyModel"] as ReportViewModel;
    return View(report);
}
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90