0

例如,我有一些带有操作链接的视图:

@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在视图完全加载后在模型中定义,并且此页面上的所有 javascripts 都已执行。这个视图可能是空的,没有任何内容,我只需要执行一些 javascript,然后将用户重定向到外部页面。怎么能做到这一点?

4

2 回答 2

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";
于 2012-11-09T16:52:46.990 回答
2

正如@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>
于 2012-11-09T16:54:10.293 回答