2014-10-22 30 views
1

我要去下面的步驟:在查看mvc4使用的Response.Redirect

  1. 在控制器動作一號重定向查看NO1

    ;

  2. 鑑於一號

    我想用

    @{Response.Redirect(Url.Action("CreatePdf", "Home");} 
    

指令顯示CSHTML頁面旁邊我想重定向到新的動作二號;

  1. 行動2已達成,我得到了我的結果(pdf文件),但我不能看到我稱之爲此行爲的視圖1號。 如何加載這個視圖並顯示html頁面?
+0

重定向會導致瀏覽器加載新頁面,您希望發生什麼? – DavidG 2014-10-22 09:25:07

+0

我不想加載新頁面,而是觸發新的操作(並顯示當前頁面) – arth81 2014-10-22 09:26:47

+0

您想要在顯示頁面的同時將PDF下載到客戶端嗎? – DavidG 2014-10-22 09:27:41

回答

1

只是一個小調整,以@ DavidG的回答是:

<script type="text/javascript"> 
    $(document).ready(function() { 
     setTimeout(DownloadPdf, 1000); 
    }); 
    function DownloadPdf() { 
     location.href = "@Url.Action("CreatePdf", "Home")"; 
    } 
    </script> 

只是測試和工作。它會在1秒後下載文件

+0

非常感謝,這對我很有用! – arth81 2014-10-22 11:20:51

1

重定向導致整個會話被定向到新的頁面並且丟失了你發送的任何東西。我會使用jQuery來代替:

<script type="text/javascript"> 
    $(document).ready(function() { 
     setTimeout(DownloadPdf, 1000); 
    }); 

    function DownloadPdf() { 
     window.location = "@Url.Action("CreatePdf", "Home")"; 
    } 
</script> 
+0

不幸的是,現在我到達視圖,但沒有發生任何動作。 – arth81 2014-10-22 09:42:26

+0

嘗試一下像編輯一樣的超時 – DavidG 2014-10-22 09:43:42

+0

仍然一樣,即使我加倍超時。 – arth81 2014-10-22 09:50:34

0
I would suggest : 

public ActionResult ControllerAction1() 
{ 
    return View(); 
} 

For the View(), for document.ready function : 
$(document).ready(function() { 
    $.ajax({ 
     url: '@Url.Action("Action2", "Controller")', 
     contentType: 'application/json; charset=utf-8', 
     type: 'POST', 
     dataType: 'html', 
     data: JSON.stringify(model) 
    }) 
    .success(function(result) { 
     // return true or false 
     // html of json result 
    }) 
    .error(function(xhr, status) { 

    }); 

});