Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodically Refresh a partial view ( ASP.Net MVC)

I need a periodic refresh of .Net's partial view. It is working with Ajax.ActionLink, is there a similar feature for periodic refresh? Can I do it without using jQuery?

like image 306
Mr. Zen Avatar asked Jul 28 '11 14:07

Mr. Zen


People also ask

Can we return partial view in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

What is advantage of partial view in MVC?

The major advantage of partial view is that we can reuse the partial view logic.


1 Answers

Zen, you could do it by a code like this:

function loadPartialView() {
   $.ajax({
    url: "@Url.Action("ActionName", "ControllerName")",
    type: 'GET', // <-- make a async request by GET
    dataType: 'html', // <-- to expect an html response
    success: function(result) {
                $('#YourDiv').html(result);
             }
   });
}

$(function() {

   loadPartialView(); // first time

   // re-call the function each 5 seconds
   window.setInterval("loadPartialView()", 5000);

});

Remember your Action should return a PartialView. I hope it helps you!

like image 156
Felipe Oriani Avatar answered Nov 04 '22 10:11

Felipe Oriani