2014-08-28 83 views
0

我正在編寫一個報告工具,您可以直接在頁面上顯示報告或將其下載爲Excel文件。如果你只是想在網頁上顯示它,網站會按預期重新加載。如果它作爲excel文件下載,則下載可以正常工作,但頁面不會重新加載。這對我來說是一個問題,因爲我不知道如何在之後禁用加載動畫。下載是通過對響應對象的寫入操作完成的。下面是代碼:通過回覆文件下載後重新加載頁面

private void ExcelExport(DataTable outList) 
{ 
    ViewBag.Reload = true; 
    Response.ClearContent(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition", "attachment; filename=report.xls"); 
    Response.ContentType = "application/ms-excel"; 
    Response.Charset = ""; 
    Response.Output.Write(Excel.GetXml(outList)); 
    Response.Flush(); 
    Response.End(); 
} 

的代碼是從一個ActionResult方法我的控制器(其中爲ExcelExport方法也設)中調用:

public ActionResult WisPriceMatrix(string cc, string sl, string exp) 
{ 
    ViewBag.StorageLocations = this.StorageLocations; 
    ViewBag.WisPriceMatrixReport = null; 
    int cleanCode = 3; 
    if ((cc != null && cc != string.Empty && Int32.TryParse(cc, out cleanCode)) || (sl != null && sl != string.Empty)) 
    { 
      sl = sl == string.Empty ? null : sl; 

      ViewBag.ParameterSl = sl; 
      ViewBag.ParameterCleanCode = cleanCode; 

      DataTable outList = dc.GetWisPriceMatrix(sl, cleanCode); 

      if (exp == null || exp == string.Empty || exp != "1") 
      { 
       ViewBag.WisPriceMatrixReport = outList; 
      } 
      else 
      { 
       if (outList.Count > 0) 
       { 
        this.ExcelExport(outList); 
       } 
       else 
       { 
        ViewBag.NoResults = "1"; 
       } 
      } 
    } 

    return View(); 
} 

任何想法我怎麼能強制頁面重新加載之後?

我試圖創建一個ViewBag變量,它會指示重載是neede並通過JavaScript對它做出反應,但由於頁面沒有刷新,所以這是nu成功;-)。

+0

在這裏發佈更多的代碼誰正在調用ExcelExport方法...... ???? – 2014-08-28 11:39:06

+0

我用相應的代碼編輯了我的問題。 – 2014-08-28 11:54:29

+0

不能明白你想要什麼? – 2014-08-28 11:55:47

回答

0

在你的情況下,爲了重新加載頁面要麼你可以使用Viewbag並設置Viewbag值控制器上說Viewbag.data="reload",然後在視圖檢查Viewbag作爲

$(document).ready(function(){ 
    if('@Viewbag.data' == "reload") 
    { 
    window.location.reload(true); 
    } 
}); 

或者你可以只是代替return View()使用return RedirectToAction("WisPriceMatrix")RedirectToAction創建一個新的http(302)請求並重新加載頁面。

+0

我已經嘗試了類似於第一個建議somethig。但是這不起作用,因爲該站點未被重新加載,因此腳本不會再次執行。我剛剛嘗試了第二個不起作用的建議。到達命令'return RedirectToAction(「WisPriceMatrix」);',但對結果沒有影響。 – 2014-08-28 12:15:57

+0

@RomanoZumbé...第二個答案肯定會出手工作...只是檢查你的httpget和httppost控制器的行爲有名稱「WisPriceMatrix」.. ?? – 2014-08-28 12:18:51

+0

控制器Action被命名爲「WisPriceMatrix」,但我認爲在手動編輯響應之後,「RedirectToAction」方法的行爲與預期不符。 – 2014-08-28 12:30:20