简体   繁体   English

如何在Response.End()之后重定向到MVC中的另一个页面

[英]how to redirect to another page in MVC after Response.End()

In my project the user can search the details of the school students and export the same to excel. 在我的项目中,用户可以搜索学校学生的详细信息,并将其导出到Excel。 I am using OpenOfficeXMl for achieving this. 我正在使用OpenOfficeXMl实现这一目标。 The excel is downloading with the data once the search button is clicked. 单击搜索按钮后,Excel将随数据一起下载。

Is it possible to redirect to an another page after the excel download is completed. excel下载完成后是否可以重定向到另一个页面。

I am assigning the data to a datatable, then it is extracting to the excel as below. 我将数据分配给数据表,然后将其提取到excel,如下所示。

using (ExcelPackage pck = new ExcelPackage())
{
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("PData");
    ws.Cells["A1"].LoadFromDataTable(p_data, true);
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=School-" + DateTime.Now + "-file" + ".xlsx");
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
}

Is it possible to do a return View() or Redirect after Response.End() ? 是否可以在Response.End()之后执行return View()Redirect

Edit 编辑

After the excel is downloaded I need to navigate to another page to show the same data which is exported in excel. 下载excel后,我需要导航到另一个页面以显示在excel中导出的相同数据。 like return View(Data) where Data is a IpagedList Object return View(Data) ,其中DataIpagedList对象

You cannot send an excel file to the response and at the same time redirect. 您不能将excel文件发送到响应,并且不能同时重定向。

In this case, an approach could be to simply return a View, and pass a view model that contains your IpagedList as well as the link to your pdf. 在这种情况下,一种方法可能是简单地返回一个View,然后传递一个包含IpagedList以及pdf链接的视图模型。 No need for a redirect; 无需重定向; unless your previous action was a POST. 除非您之前的操作是POST。

In the resulting page you then can show the IpagedList information and also a link they can click download the excel file, or even download this automatically from jquery, for example by opening a new window with the URL pointing to your pdf. 然后,在显示的页面中,您可以显示IpagedList信息,以及他们可以单击下载excel文件的链接,甚至可以从jquery自动下载该文件,例如,通过打开一个新窗口,并指向pdf的URL。

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

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