tags:

views:

49

answers:

1

I'm in the process of converting my asp.net app to asp.net mvc. I'm using a master page which contains a div that currently renders a partial view. This partial view displays the model data that I want displayed.

I saved the "best" for last and that is to convert an asp.net timer triggered update panel to MVC. I'm an asp.net MVC newbie. Any suggestions? I've seen an example in this forum that uses a form and a submit button. How do I add a timer? Do I simply set up a timer in my controller and every time in fires return my partial view? Do I even need the form? Thanks

+5  A: 

Triggering AJAX requests at regular intervals to a controller action that returns a partial view should suffice:

public ActionResult SomePartial()
{
    // return PartialView.ascx partial view containing an HTML fragment
    return PartialView();
}

And in your javascript:

$(function() {
    // trigger an ajax request to the controller action every 5s
    // and inject the returned HTML fragment into a div with id="result"
    window.setInterval(function() {
        $('#result').load('/home/somepartial');
    }, 5000);
});

Should update div tag with id="result" in the markup:

<div id="result"></div>
Darin Dimitrov
I never seem to get that function to execute.
Have you included `jQuery` scripts in your view?
Darin Dimitrov
I have now. Thank you very much. Works great and very simple.
I have a related question. How do I get that function to fire as soon as the page is loaded?
Just call `$('#result').load('/home/somepartial');` outside of `window.setInterval`. Of course in this case it would be better to externalize this into a separate function which you would call once inside the timer callback and once the page is loaded to avoid repeating code.
Darin Dimitrov
When I externalize the call to load the div, the timer never calls it. Here's my code:The first function runs as the page loads, which is fine. As does the second,but after 5 seconds injectQuote doesn't get called. What am I missing? Thanks.$(function injectQuote() { $('#randomQuote').load('/updatequote'); });$(function triggerQuote() { window.setInterval(function triggerQuote() { injectQuote(); }, 5000); });
Unfortunately, the AJAX requests are not being triggered in IE8.
Once I disabled caching all is well. Stupid IE caching...
I added this attribute to my updatequote (aks somepartial) action.[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]