简体   繁体   English

似乎无法将模型传递给ActionAsPdf

[英]Can't seem to pass a model to an ActionAsPdf

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. 我需要为每个合作伙伴的AR生成一个pdf副本,为此,我试图通过TempData将模型传递给另一个视图,但是它始终返回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. 但是,当到达AccountsReceivableReport时,临时数据始终为null。 I can just recall the SP on the AccountsReceivableReport() but that would take longer. 我可以回想起AccountsReceivableReport()上的SP,但这会花费更长的时间。

Is there an issue with passing a tempdata to an ActionAsPdf? 将临时数据传递给ActionAsPdf是否存在问题? I'm using rotativa btw. 我正在使用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. 当我插入一个断点时,TempData [“ MyModel”]被成功地填充了结果,但是当我到达AccountsReceivableReport_PerPartner()的方法时,它现在为null。

If i try to change it from ActionAsPdf to ViewAsPdf it returns an error on the buildPdf part 如果我尝试将其从ActionAsPdf更改为ViewAsPdf,则会在buildPdf部分返回错误

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary. ASP.NET MVC中的TempData基本上是从TempDataDictionary派生的字典对象。 TempData stays for a subsequent HTTP Request as opposed to other options (ViewBag and ViewData) those stay only for current request. TempData保留用于后续的HTTP请求,而其他选项(ViewBag和ViewData)则仅针对当前请求保留。 detail 详情

Try session for this: 为此尝试会议:

public ActionResult AccountsReceivableReport_PerPartner(string employeecode)
{
    var report = Session["MyModel"] as ReportViewModel;
    return View(report);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM