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.

5
  • 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. Commented Jan 12, 2017 at 19:37
  • 3
    "I get exception" - do you think that including at least exception message in your question would be useful? Commented Jan 12, 2017 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. Commented Jan 12, 2017 at 19:43
  • Possible duplicate of Page Redirect after X seconds wait using JavaScript Commented Jan 12, 2017 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. Commented Jan 12, 2017 at 19:59

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

8 Comments

Shall I put this code in my cshtml file under script tag?
Yes you can, but best practice is to make new javascript file and then reference it in cshtml file
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.
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
I highly recommend you to read some basic usage of Javascript and jQuery (for example on w3schools.com ) Then it will be easy to fit this to your application :)
|
2
TimeSpan ticksRemaining = DateTime.Now - (DateTime)Session["startTime"];
int x = ticksRemaining.TotalSeconds;

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.