1

In payment window When client clicks on payment button I want to start a timer. And if the payment processing takes more than let's say 5 second I want to redirect to a page. Below is what I could think of.

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SavePayment(PaymentFormMV data)
        {
            if (Session["startTime"] == null)
            {
                Session["startTime"] = DateTime.Now;
            }

            var ticksRemaining = DateTime.Now - (DateTime)Session["startTime"];
           int x = int.Parse(ticksRemaining.ToString()); // Format Unhandled exception
            if(x == 5)
            {
                return RedirectToAction("Index", "Home");
            }
            // Payment Logic Here to 3rd Party API
             return View("PaymentConfirmation", returnData);
}

But it's not working as I expect when I calculate ticksRemaining. Do I need some theading or something here. I am new to development please guide me. I just want see the time duration between button click and current time. If it crosses 5 second I want to redirect the user to a new page.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • What Exception is it throwing? Why not do this as something on the client where the page requests a redirect? However, with either solution, you won't be stopping the execution of any long running task. The way that you have it set up now, the user would have to click the button twice for anything to work. – krillgar Jan 12 '17 at 19:37
  • 3
    "I get exception" - do you think that including at least exception message in your question would be useful? – Igor Jan 12 '17 at 19:37
  • @krillgar : I am beginner trying to learn. Can you guide me if its better to do it on client side. if so can you provide any link. And I have edited my code. Apparently, my approach is not correct. – Unbreakable Jan 12 '17 at 19:43
  • Possible duplicate of [Page Redirect after X seconds wait using JavaScript](http://stackoverflow.com/questions/17150171/page-redirect-after-x-seconds-wait-using-javascript) – krillgar Jan 12 '17 at 19:53
  • After you fix your code by using Igor's suggestion you should change `if(x == 5)` to `if(x >= 5)` in case your method is called 6+ seconds later. – Quantic Jan 12 '17 at 19:59

3 Answers3

3

It is much more reasonable to implement this on client side. You can use JavaScript. For example this might help you:

Page Redirect after X seconds wait using JavaScript

JQuery Redirect to URL after specified time

Community
  • 1
  • 1
MacakM
  • 1,804
  • 3
  • 23
  • 46
  • Shall I put this code in my cshtml file under script tag? – Unbreakable Jan 12 '17 at 19:47
  • Yes you can, but best practice is to make new javascript file and then reference it in cshtml file – MacakM Jan 12 '17 at 19:49
  • In the link you have mentioned its all based on page load. But for me the timer should start on button click. Also, to state the obvious I would want the button click to perform its normal POST operation just like MVC and hit the controller function. – Unbreakable Jan 12 '17 at 19:54
  • When you use JavaScript it does not imply that you can't perform POST operation... it is difference between client side and server side execution – MacakM Jan 12 '17 at 19:56
  • I highly recommend you to read some basic usage of Javascript and jQuery (for example on http://www.w3schools.com/ ) Then it will be easy to fit this to your application :) – MacakM Jan 12 '17 at 19:58
  • So I have added an onclick event on form submit. But inside that do I need to make an "http request call"to redirect to a particular page. Or I should simply hardcode "www.bla.home/index". I just want the user to get redirected to home page if a certain amount of time has passed. – Unbreakable Jan 12 '17 at 20:07
  • Just use search on SO :) http://stackoverflow.com/questions/8952953/calling-asp-net-mvc-action-methods-from-javascript – MacakM Jan 12 '17 at 20:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133031/discussion-between-unbreakable-and-macakm). – Unbreakable Jan 12 '17 at 20:15
2
TimeSpan ticksRemaining = DateTime.Now - (DateTime)Session["startTime"];
int x = ticksRemaining.TotalSeconds;
Igor
  • 15,833
  • 1
  • 27
  • 32
0

in the controller just use some thing like bellow

public ActionResult YourcurrentAction()
{
   TempData["msg"] = "Your desire message here";
   return RedirectToAction("Action_Which_You_Gonna_Redirect_To", "YourControlName");
}

now in redirected action use code bellow

<center> <h2> @TempData["msg"].ToString() </h2> </center>
 @TempData.Remove("msg")

    <script type="text/javascript">
        window.setTimeout(function () {
            window.location.href = "Your_Desire_Link_To_Redirect";
        }, 5000);
    </script>

If you need just client side use my code second part . other wise use both but the way you need.

by the way Remember that if you are moving between diffrent actions you must use TempData to have stored values like message in our example. and remember to remove it for freeing memory after usage. otherwise Use ViewBag.

Wish it help you, Heydar;

Heydar
  • 137
  • 1
  • 8