2012-11-09 21 views
0

例如,我有一些看法與操作鏈接:如何在加載後將用戶重定向到視圖中的另一個頁面是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,在someModelForView模型中定義後的看法是完全加載,並在此頁面上的所有JavaScript被執行。這個視圖可能是空的,沒有任何內容,我只需要執行一些JavaScript,然後將用戶重定向到外部頁面。如何做到這一點?

+0

(文件)。就緒() – paragy

+1

爲什麼它必須得到在JavaScript中完成,如果沒有內容?爲什麼不使用c#? –

回答

3

一旦視圖被渲染並載入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"; 
2

你可以直接進行重定向,如@achristov已建議。但是,如果你必須返回SomeView執行您可以使用此JavaScript的:使用jQuery您可以$

@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> 
+0

這是一個XSS洞。 – SLaks

+0

你必須能夠操縱'模型'來利用它。 – Alex

+0

'模型'將經常包含用戶數據(例如標題) – SLaks

相關問題