我的应用程序是MVC3(ASPX视图)。 我有一个窗体,在"编辑"视图中,我有保存按钮,用于保存窗体并返回到索引页,另一个按钮用于在与"编辑"相同的控制器中从另一个 ActionResult 打印 PDF;有没有办法从一个按钮保存和打印 PDF?这是我的编辑视图脚本:
public ActionResult EditCTAH(long learnerID = 0, long caseListID = 0)
{
ViewBag.caseListID = (long)Session["_CTA_CaseListId"];
try
{
CTAHFormEdit ctform = _learnerscaseListsSvc.GetCTAHForm(learnerID, caseListID);
return View(ctform);
}
catch
{
return null;
}
}
[HttpPost]
public ActionResult EditCATH(CTAHFormEdit ctform)
{
if (ModelState.IsValid)
{
_learnerscaseListsSvc.EditCTAHForm(ctform);
long courseId = (long)Session["_CTA_CourseId"];
long caseId = (long)Session["_CTA_CaseId"];
long caselistId = (long)Session["_CTA_CaseListId"];
return RedirectToAction("Index", new { courseId = courseId, caseId = caseId, caselistId = caselistId });
}
return View(ctform);
}
以下是打印 PDF 的脚本:
public ActionResult EvaluationCATH_PDF(long learnerID = 0, long caseListID = 0)
{
try
{
.....
pdfStamper.Close();
byte[] byteInfo = workStream.ToArray();
SendPdfToBrowser(byteInfo, cth.Learner_ID, cth.StudyCase_ID);
return null;
}
catch
{
return null;
}
}
提前谢谢。
如果您想在编辑操作的保存中获取PDF,您可以重定向到将PDF返回到浏览器的操作方法
[HttpPost]
public ActionResult EditCATH(CTAHFormEdit ctform)
{
if (ModelState.IsValid)
{
//Saved succesfully, lets show the pdf in browser.
return RedirectToAction("Print",new { id=someIdValue});
}
return View(ctform);
}
public ActionResult Print(string id)
{
byte[] pdfByteArray=GetByteArrayFromPassedID(id);
return File(pdfByteArray,"application/pdf","somefriendlyname.pdf");
}