簡體   English   中英

ASP .NET MVC如何在加載后將用戶從視圖重定向到另一個頁面?

[英]How to redirect user to another page from view after it was loaded is ASP .NET MVC?

例如,我有一些帶有動作鏈接的視圖:

@Html.ActionLink("Action", "Controller")

Action動作返回一些看法:

public ActionResult Action()
{
    string someModelForView = "some url i need to redirect after view was fully loaded";
    return View("SomeView", someModelForView);
}

我需要的是將用戶重定向到URL,該URL在完全加載視圖並執行此頁面上的所有Javascript之后在someModelForView模型中定義。 該視圖可能是空的,沒有任何內容,我只需要執行一些javascript,然后將用戶重定向到外部頁面即可。 如何做到這一點?

呈現視圖並加載JavaScript后,您(服務器)已經將響應(封裝在返回的ActionResult )發送到客戶端(瀏覽器)。 因此,您不能讓ASP.NET MVC重定向您-服務器已完成對請求的處理。

不過,您可以使用JavaScript重定向:

// Here goes your JavaScript code that needs to be executed
// ...

// ... and here comes the redirect:
window.location.href = "http://newurl.com";

您可以按照@achristov的建議直接進行重定向。 但是,如果必須返回SomeView來執行javascript,則可以使用以下代碼:

@model string
<html>
    <head>
    </head>
    <body>
        <script type="text/javescript">
            $(document).ready( function() {
                // all your javascript code...
                // ...and then:
                window.location = "@Model";
            });
        </script>
    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM