我需要生成每个伙伴的AR的pdf副本,为此我试图通过TempData将模型传递给另一个视图,但它总是返回null。
以下是我的代码。
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);
}
但是当它到达AccountsReceivableReport时,tempdata始终为null。 我可以回想一下AccountReceivableReport()上的SP,但这需要更长的时间。
将tempdata传递给ActionAsPdf是否存在问题? 我正在使用rotativa btw。
当我插入断点时,TempData [“MyModel”]已成功填充结果,但当我到达AccountsReceivableReport_PerPartner()
的方法时,它现在为空。
如果我尝试将其从ActionAsPdf更改为ViewAsPdf,则会在buildPdf部分返回错误
答案 0 :(得分:0)
ASP.NET MVC中的TempData基本上是派生自的字典对象 TempDataDictionary。 TempData将作为后续HTTP请求保留 与其他选项(ViewBag和ViewData)相反,这些选项只适用于 当前的要求。 detail
尝试此会话:
public ActionResult AccountsReceivableReport_PerPartner(string employeecode)
{
var report = Session["MyModel"] as ReportViewModel;
return View(report);
}