0

In my controller class I return some data to my view and it's all good.

Can i do something like this?

    public ActionResult List()
    {
        while (true)
        { 

            Thread.Sleep(3000);

            return View("ListStatus", data);
        }

    }

Of course the above code won't work as when the return statement is ran the function exists.

I'm sure i can use some Ajax in the View itself to pull data up from the server every 3 second but for my current purpose it would just be easier to do what i'm attempting in the above code

Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
Billy
  • 241
  • 1
  • 4
  • 13
  • See [this answer](http://stackoverflow.com/questions/5396282/auto-refresh-in-asp-net-mvc/5396312#5396312) to a later question. – Shaul Behr Jun 05 '11 at 15:36

2 Answers2

5

It seems you're trying to do the refresh from the server side. Like 'pushing' the updates to the client. That's not how asp.net works. The client makes a request and the server then sends a response. This alone means you cant do the above.

Like jcm said, you need to have the client/browser making the follow-up requests for updated data.

I'd suggest a js/ajax/jQuery option. You can google and get heaps of examples.

Community
  • 1
  • 1
Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
  • Technically you can keep a connection open and keep appending data to it over time. But that is highly browser dependent behavior and not something you would want to do (at least with today's tech, see HTML5 Websockets). If you want a persistent connection use flash, a java applet or silverlight. – Matt Dec 10 '09 at 09:30
  • Websockets realy sounds like a nice way in the future. (future in bold) http://dev.w3.org/html5/websockets/ . This draft came out yesterday. – Mathias F Dec 10 '09 at 10:46
2

Use meta tag <meta http-equiv="refresh" in your header, if you want to refresh the whole page.

Use jquery solution, if you want to refresh parts of the page.
Auto-refreshing div with jQuery - setTimeout or another method?
http://dev.kafol.net/2008/10/jquery-update-divs-html-dynamically.html
http://docs.jquery.com/Ajax

Community
  • 1
  • 1
Christian13467
  • 5,324
  • 31
  • 34