James Peel James Peel - 2 months ago 26
jQuery Question

Auto-refresh partial view with JQuery

I have a partial view that I am loading in using:

<div class="row">
<div class="col-xs-12">
@Html.Action("NewFaults", "Fault")
</div>
</div>


This ultimately loads a partial view called
_NewFaults.cshtml
and I want to refresh every 10 seconds (for example) to regularly show any new faults that come in. I have been looking around online for how to do this for a while, but all the examples I find are very specific to their example and it's not obvious how to adapt them.

Can someone help me?

Answer

As per the comment, use a javascript timer

var timer = setInterval(YourFunction, 10000);

where YourFunction does an AJAX call to your controller for a partial view.

$.ajax({
    url: "@Url.Action("Action","Controller")",
    type: 'POST',
    dataType: 'html',        
    cache: false,
    success: function (html) {
        //show it on page
    }
});

to such a method

public PartialView Action()
{
    return PartialView();
}