views:

227

answers:

3

Hi there,

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

A: 

No. Once it has gone to the view it has gone to the view. You could do something with streaming but that would be significantly more effort than it is worth.

Your best bet is to do it in javascript with ajax.

jcm
No need of Ajax to refresh a page : if there's no need for partial refresh, a "simple" javascript will do the trick...
Cédric Rup
There's always <meta http-equiv="refresh">... it doesn't give you much flexibility though...
Richard Beier
+5  A: 

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.

cottsak
that's not how the web works, let alone asp.net.
darasd
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.
envalid
Websockets realy sounds like a nice way in the future. (future in bold) http://dev.w3.org/html5/websockets/ . This draft came out yesterday.
Malcolm Frexner
A: 

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.
http://stackoverflow.com/questions/220767/auto-refreshing-div-with-jquery
http://dev.kafol.net/2008/10/jquery-update-divs-html-dynamically.html
http://docs.jquery.com/Ajax

Christian13467