【发布时间】:2020-09-06 06:29:12
【问题描述】:
我有一个名为 ExportPagePartial 的 asp.net 核心 MVC 部分视图,它允许用户从系统中导出页面并下载它。在 HttpGet 控制器操作中,我显示部分视图(作为模态弹出窗口)以获取用户输入。
模态弹出窗口
<a class="dropdown-item" asp-action="ExportPagePartial" asp-route-userId="@Model.UserId" asp-route-businessAccountId="@Model.BusinessAccountId" asp-route-projectId="@Model.ProjectId" asp-route-pageId="@Model.PageId" data-toggle="modal" data-target="#ModalPlaceholder" title="Export page."><i class="fas fa-cloud-download-alt"></i> Export</a>
控制器获取操作
[HttpGet]
public IActionResult ExportPagePartial(string userId, string businessAccountId, string projectId, string pageId)
{
ExportPageViewModel model = new ExportPageViewModel()
{
// Set properties
};
return PartialView(nameof(ExportPagePartial), model);
}
一旦用户从模态弹出部分视图(这是一个表单提交操作)中点击导出按钮,就会正确调用以下 HTTPPost 操作。 在此操作中,我必须从 Web Api 获取文件,然后通过浏览器下载它,但是下载完成后我想关闭部分视图。下载完成后,部分视图仍然可见。
返回动作永远不会起作用,部分模态弹出视图不会关闭 return RedirectToAction(nameof(BlahRedirectAction));
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ExportPagePartial(ExportPageViewModel model)
{
// Call Web API to get the file
string downloadUrl = "blah_blah_url";
using (HttpResponseMessage httpResponse = await WebApiClient.HttpClient.PostAsJsonAsync(downloadUrl, unprotectedExportInput))
{
if (!httpResponse.IsSuccessStatusCode)
{
throw new InvalidOperationException(await httpResponse.Content.ReadAsStringAsync());
}
// Download the file now.
ActionContext actionContext = new ActionContext(HttpContext, ControllerContext.RouteData, ControllerContext.ActionDescriptor, ModelState);
FileStreamResult fileContent = File(await httpResponse.Content.ReadAsStreamAsync(), httpResponse.Content.Headers.ContentType.MediaType, httpResponse.Content.Headers.ContentDisposition.FileName);
await fileContent.ExecuteResultAsync(actionContext);
}
// Redirect to main pain
// The view never redirects and partial view is still visible
return RedirectToAction(nameof(BlahRedirectAction));
}
【问题讨论】:
-
局部视图以什么形式呈现,类似于模态弹出?能否提供全面的代码供我们参考?
-
@YongqingYu 是的,部分视图显示为模式弹出窗口,我更新了描述以包含更多详细信息。
标签: asp.net asp.net-mvc asp.net-core-mvc partial-views