0

I am trying to display a countdown timer in view. After time is out, user should be redirected to another page. The code below of course doesn't work, but it should clarify a bit what I aim for:

@{
    int txt = (int)TempData["txt"];
    DateTime st = (DateTime)HttpContext.Current.Session["start"];
    DateTime nw = DateTime.Now;
    TimeSpan es = nw - st;
}

@switch(txt)
{
    case 1:
        @Html.Partial("_Text1", Model)
        break;
    case 2:
        @Html.Partial("_Text2", Model)
        break;
}

<p>@es.ToString("mm:ss")</p>

So when the user opens the page for the first time, timer should start and be visible while user switches between subviews _Text1 and _Text2 (each subview has a link to another). When timer reaches 00:00, RedirectToAction should be invoked. If possible, I would like to avoid using JScript or any solution that a user can disable/overcome with browser settings.

Mark_856
  • 13
  • 4
  • I don't think you can redirect from server without using websockets or something like that, take a look at SignalR. I would personally recommend a javascript approach eventhough you don't like – Carlos487 Aug 14 '15 at 20:12

1 Answers1

0

One possible option that doesn't involve javascript is the HTML META tag.

example: meta http-equiv="refresh" content="5" where content is the time to redirect

So something along the lines of

<head>
<title>IU Webmaster redirect</title>
<META http-equiv="refresh" content="5;URL=http://yourpage">

content is specified in seconds. META elements live within the HEAD element.

For more information refer to: this article

Maz H
  • 151
  • 8
  • That's a good idea, but would the model from the view be passed as well in this case? – Mark_856 Aug 14 '15 at 22:10
  • Hey Mark, not sure but this link may help http://stackoverflow.com/questions/31806548/net-mvc-pass-data-from-child-view-to-layout-view-dynamic-meta-tag-updates – Maz H Aug 15 '15 at 11:40
  • TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this. Therefore, the only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view. – Maz H Aug 15 '15 at 11:43
  • I could arrage for TempData to be passed to all possible redirects, since the app is meant to go invalid if user manually changes URL. But every time I would need to check previously lapsed time, since content in metadata needs to be an exact number and not some variable, right? I'm inclined to try javascript, but have no knowledge in that area. Got any JS ideas :) ? – Mark_856 Aug 15 '15 at 12:26
  • This should put you in the right direction: http://stackoverflow.com/questions/24700208/mvc-redirect-to-another-page-after-a-four-second-pause – Maz H Aug 15 '15 at 13:29