暂停四秒钟后,MVC重定向到另一个页面

8 javascript asp.net asp.net-mvc razor asp.net-mvc-4

我对MVC比较新,但我有一个问题:

目前,当用户第一次登录我们的网站时,他们会被重定向到更改密码页面; 他们必须先更改密码才能访问网站的任何主要功能.在成功更改密码后,我想在更改密码页面上显示我的自定义"密码成功更改"消息; 我现在这样做.在我的更改密码页面上显示自定义消息后,我想等待几秒钟,然后将它们重定向到主页.

我希望能够做到这样的事情:

//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");
Run Code Online (Sandbox Code Playgroud)

谢谢!

ryu*_*ice 13

您无法在服务器端执行此操作,您必须使用javascript.您可以使用

window.setTimeout(function(){
window.location.href='your URL';
}, 4000);
Run Code Online (Sandbox Code Playgroud)


Mic*_*ook 5

您可以考虑的另一种方法是旧学校元刷新

<meta http-equiv="refresh" content="4; url=http://example.com/">
Run Code Online (Sandbox Code Playgroud)

您可以使用模型上的属性或ViewBag中的值来控制

@if(Model.DoRedirect) {
    <meta http-equiv="refresh" content="4; url=http://example.com/">
}
Run Code Online (Sandbox Code Playgroud)

由于这需要在标题中,您可能需要在_layout中添加标题"section"

<head>
    ...
    @RenderSection("header", required: false)
</head>
Run Code Online (Sandbox Code Playgroud)

然后你可以在你的视图中使用它

@section header
    @if(Model.DoRedirect) {
        <meta http-equiv="refresh" content="4; url=http://example.com/">
    }
}
Run Code Online (Sandbox Code Playgroud)