这可能不是您想要的,但可能满足您的需要。您可以将 PDF 嵌入到局部视图中,然后通过 ajax 使用表单提交按钮上的 PDF 更新局部视图。
示例代码:局部视图
@model Test.Models.ViewModel
<style type="text/css">
#pdfbox
{
width:600px;
height:400px;
border: 5px solid #ccc;
}
</style>
<object id='pdfbox' type="application/pdf" data="@Url.Action("GeneratePDF", "Home", Model)">
Click @Html.ActionLink("here", "GeneratePDF", "Home") to view the file.
</object>
控制器调用:
public ActionResult GeneratePDF(ViewModel model)
{
byte[] bytes = OpenPDFAndGetBytes("Thepdfname");
return File(bytes, "application/pdf");
}
public ActionResult RenderPDF(LabelViewModel model)
{
return PartialView(model);
}
主要观点:
@using (Ajax.BeginForm("RenderPDF", "Home", new AjaxOptions { UpdateTargetId = "pdf" }))
{
<table>
<tr>
<td>
<fieldset>
<legend>Fill the form:</legend>
Some form junk can go here
<br />
<input type="submit" value="Display PDF" />
</fieldset>
</td>
<td>
<div id='pdf'>
@{
Html.RenderPartial("RenderPDF", Model);
}
</div>
</td>
</tr>
</table>
}
(编辑:将“主视图”更改为标题)