我要去下面的步驟:在查看mvc4使用的Response.Redirect
- 在控制器動作一號重定向查看NO1
;
- 鑑於一號
我想用
@{Response.Redirect(Url.Action("CreatePdf", "Home");}
指令顯示CSHTML頁面旁邊我想重定向到新的動作二號;
- 行動2已達成,我得到了我的結果(pdf文件),但我不能看到我稱之爲此行爲的視圖1號。 如何加載這個視圖並顯示html頁面?
我要去下面的步驟:在查看mvc4使用的Response.Redirect
;
我想用
@{Response.Redirect(Url.Action("CreatePdf", "Home");}
指令顯示CSHTML頁面旁邊我想重定向到新的動作二號;
只是一個小調整,以@ DavidG的回答是:
<script type="text/javascript">
$(document).ready(function() {
setTimeout(DownloadPdf, 1000);
});
function DownloadPdf() {
location.href = "@Url.Action("CreatePdf", "Home")";
}
</script>
只是測試和工作。它會在1秒後下載文件
非常感謝,這對我很有用! – arth81 2014-10-22 11:20:51
重定向導致整個會話被定向到新的頁面並且丟失了你發送的任何東西。我會使用jQuery來代替:
<script type="text/javascript">
$(document).ready(function() {
setTimeout(DownloadPdf, 1000);
});
function DownloadPdf() {
window.location = "@Url.Action("CreatePdf", "Home")";
}
</script>
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) {
});
});
重定向會導致瀏覽器加載新頁面,您希望發生什麼? – DavidG 2014-10-22 09:25:07
我不想加載新頁面,而是觸發新的操作(並顯示當前頁面) – arth81 2014-10-22 09:26:47
您想要在顯示頁面的同時將PDF下載到客戶端嗎? – DavidG 2014-10-22 09:27:41