javascript MVC 在四秒暂停后重定向到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24700208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC redirect to another page after a four second pause
提问by ryudice
I'm relatively new to MVC, but I have a problem:
我对 MVC 比较陌生,但我有一个问题:
Currently, when a user signs into our site for the first time, they are redirected to a change password page; they must change their password before they can access any of the main functionality of the site. After they change their password successfully though, I want to show my custom "Password successfully changed" message on the change password page; I currently do this. After I display my custom message on my change password page, I want to wait a few seconds and then redirect them to the home page.
目前,当用户第一次登录我们的网站时,他们会被重定向到更改密码页面;他们必须先更改密码,然后才能访问网站的任何主要功能。在他们成功更改密码后,我想在更改密码页面上显示我的自定义“密码已成功更改”消息;我目前这样做。在更改密码页面上显示自定义消息后,我想等待几秒钟,然后将它们重定向到主页。
I want to be able to do something like this:
我希望能够做这样的事情:
//User successfully changes password on ChangePassword page
return View(); //This keeps them on the ChangePassword page and shows the success message
//Wait five seconds on ChangePassword page
//Redirecttoaction("Index", "Home");
Thanks!
谢谢!
回答by ryudice
You wont be able to do that on the server side, you'll have to use javascript. You can use
您将无法在服务器端执行此操作,您必须使用 javascript。您可以使用
window.setTimeout(function(){
window.location.href='your URL';
}, 4000);
回答by Michael Cook
Another method you could consider is an old school meta-refresh
您可以考虑的另一种方法是旧式元刷新
<meta http-equiv="refresh" content="4; url=http://example.com/">
Which you could control with a property on your model or a value in the ViewBag
您可以使用模型上的属性或 ViewBag 中的值来控制
@if(Model.DoRedirect) {
<meta http-equiv="refresh" content="4; url=http://example.com/">
}
Since this needs to be in the header, you might need to put a header "section" in your _layout
由于这需要在标题中,您可能需要在 _layout 中放置一个标题“部分”
<head>
...
@RenderSection("header", required: false)
</head>
Which you can then use in your view like
然后您可以在您的视图中使用它
@section header
@if(Model.DoRedirect) {
<meta http-equiv="refresh" content="4; url=http://example.com/">
}
}
回答by Ross Bush
You will have to do this on the client as your server processing ends at return View(); for the request. You have a few options on the client, for example, use a javacript timer and after so many miliseconds invoke your next action.
您必须在客户端上执行此操作,因为您的服务器处理在 return View(); 处结束;对于请求。您在客户端有几个选项,例如,使用 javacript 计时器,并在这么多毫秒后调用您的下一个操作。
function onTimerElapsed(){
window.location='@Url.Action("SomeAction","SomeController")';
}
回答by David
The "wait 4 seconds" part happens client-side, so the resulting redirect should also be client-side. You can redirect in JavaScript after 4 seconds with something like this:
“等待 4 秒”部分发生在客户端,因此产生的重定向也应该是客户端。您可以在 4 秒后在 JavaScript 中重定向,如下所示:
setTimeout(function () {
window.location.replace('@Url.Action("Index", "Home"')
}, 4000);
Note the use of the Url.Action()
helper method to inject the resulting URL into the JavaScript code.
请注意使用Url.Action()
helper 方法将结果 URL 注入 JavaScript 代码。