简体   繁体   中英

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. I am using OpenOfficeXMl for achieving this. The excel is downloading with the data once the search button is clicked.

Is it possible to redirect to an another page after the excel download is completed.

I am assigning the data to a datatable, then it is extracting to the excel as below.

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() ?

Edit

After the excel is downloaded I need to navigate to another page to show the same data which is exported in excel. like return View(Data) where Data is a IpagedList Object

You cannot send an excel file to the response and at the same time redirect.

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. No need for a redirect; unless your previous action was a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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