MVC: How to refresh a view
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);
开发者_Go百科 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
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.
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
精彩评论