Update Partial View Periodically (ASP.Net MVC)

I need to periodically update a partial .Net view. It works with Ajax.ActionLink, is there a similar function for periodic updates? Can I do this without using jQuery?

+6
source share
3 answers

Zen, you can do this with 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 that your action should return a PartialView. Hope this helps you!

+9
source

Perhaps this may help you. What version of MVC are you using? You can set the specified time interval for the helper method. This is the only way I've seen without using js.

+2
source

Try it.

  $(document).ready(function () { var url = "@(Html.Raw(Url.Action("ActionName", "ControllerName")))"; $("#PartialViewDivId").load(url); setInterval(function () { var url = "@(Html.Raw(Url.Action("ActionName", "ControllerName")))"; $("#PartialViewDivId").load(url); }, 30000); //Refreshes every 30 seconds $.ajaxSetup({ cache: false }); //Turn off caching }); 

It makes the initial call to load the div, and then subsequent calls have an interval of 30 seconds.

In the controller section, you can update the object and transfer the object to a partial view.

 public class ControllerName: Controller { public ActionResult ActionName() { . . // code for update object . return PartialView("PartialViewName", updatedObject); } } 
0
source

Source: https://habr.com/ru/post/893787/


All Articles