MVC redirects to another page after a four second pause

I'm relatively new to MVC, but I have a problem:

Currently, when a user first visits our site, they are redirected to a page with a password for changes; they must change their password before they can access any of the basic functions of the site. However, after they successfully changed their password, I want to show my custom message "Password successfully changed" on the password change page; I am currently doing this. After I post my user message on the password change page, I want to wait a few seconds, and then redirect them to the home page.

I want 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!

+6
source share
4 answers

You will not be able to do this on the server side, you will have to use javascript. you can use

 window.setTimeout(function(){ window.location.href='your URL'; }, 4000); 
+10
source

Another method you might consider is an old-school meta update

 <meta http-equiv="refresh" content="4; url=http://example.com/"> 

What can you control using the property on your model or the value in the ViewBag

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

Since this should be in the header, you may need to place the header section in your _layout

 <head> ... @RenderSection("header", required: false) </head> 

What you can use in your view, for example

 @section header @if(Model.DoRedirect) { <meta http-equiv="refresh" content="4; url=http://example.com/"> } } 
+3
source

You will need to do this on the client when the processing of your server ends with the return of View (); for request. You have several options on the client, for example, use the javacript timer and after many milliseconds trigger your next action.

 function onTimerElapsed(){ window.location='@Url.Action("SomeAction","SomeController")'; } 
+2
source

The "wait 4 seconds" part occurs on the client side, so the redirect should also be on the client side. You can redirect to JavaScript after 4 seconds with something like this:

 setTimeout(function () { window.location.replace('@Url.Action("Index", "Home"') }, 4000); 

Note the use of the Url.Action() helper method to enter the resulting URL into JavaScript code.

+1
source

Source: https://habr.com/ru/post/972088/


All Articles