【发布时间】:2019-10-31 14:42:04
【问题描述】:
在我的 Asp.Net Core(MVC,core 2.2)中,我在整个网站中使用了一种通用的模式弹出窗口。
在布局页面中,我有模态的基本代码:
<div class="modal fade" id="modalGeneric" role="dialog"></div>
function showGenericModal(event) {
var src = event.target || event.srcElement;
// find the parent that contains proper decorations
var url = $(src).closest("*[data-url]").data('url');
// trigger an HTTP GET
$.get(url, function (data) {
// fill content and show modal
$('#modalGeneric').html(data);
$('#modalGeneric').modal('show');
});
}
然后我可以使用如下链接调用它(打开):
<a class="ml-1" data-toggle="tooltip" title="Predogled"
href="@Url.Action("PdfPartial", "ZavarovalnePolice", new { id = item.Id })"
onclick="showGenericModal(event);">
</a>
这会从控制器调用一个动作并打开所需的包含其余模式代码的 PartialView。它有效。
按照这篇文章中的示例:How to display .pdf file in modal window? 我想在我的模态弹出窗口中显示 PDF。
所以,我将这两个动作添加到我的控制器中:
public IActionResult PdfPartial(int id)
{
ViewBag.Id = id;
return PartialView("_PartialDocumentPreview");
}
public async Task<IActionResult> ShowModalDocument(int id)
{
var file = _docs.GetDocument(id);
file.Contents = await _docs.Download(doc);
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = file.Title,
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
var memory = new MemoryStream(file.Contents);
return File(memory.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf);
}
和 PartialView(“_PartialDocumentPreview”):
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<div id="PdfModal">
<embed src="@Url.Action("ShowModalDocument", "ZavarovalnePolice",
new { id = @ViewBag.Id })" type="application/pdf"
style="width:100%; height:100%"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-warning"
data-dismiss="modal">Close</button>
</div>
</div>
</div>
PDF 打开,但不是在模式弹出窗口中,而是作为一个新页面(带有一个类似的 URL:https://localhost:123/ZavarovalnePolice/PdfPartial/1。
我做错了什么,它没有作为弹出窗口打开?
编辑(根据下面答案中的@Rena 建议)-
我刚刚将 href 更改为 data-url 并且可以正常工作:
<a class="ml-1" data-toggle="tooltip" title="Predogled"
data-url="@Url.Action("PdfPartial", "ZavarovalnePolice", new { id = item.Id })"
onclick="showGenericModal(event);">
</a>
【问题讨论】:
-
你能分享更多关于你的控制器和整个视图代码的细节吗?关于你的
_docs和item.id是什么。 -
_docs是一个存储库,其中包含从数据库返回文档(附件)数据的方法,item.id是视图模型中文档的 ID。我会从下面的答案尝试你的解决方法
标签: jquery asp.net-core modal-dialog