Calling ASP.NET MVC Controller explicitly via AJAX

橙三吉。 提交于 2019-12-08 03:54:21

问题


I know that I can use following piece of code to refresh a div:

<%=Ajax.ActionLink( "Update", "Administration", new AjaxOptions { UpdateTargetId = "grid", LoadingElementId = "grid-wait" } ) %>

But this creates a link; user will have to click on it to get the view refreshed.

How can I make it automatic, i.e., like say if I want the grid to be refreshed after every five seconds?


回答1:


Try this:

<p id="CurrentDateTime"></p>

<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
    window.setInterval(updateDateTime, 5000);
    function updateDateTime() {
        $.get("GetCurrentDate?rnd=" + Math.random(1000), {}, function (r) {
            $("#CurrentDateTime").html(r);
        }, "text");
    }
</script>

public ActionResult GetCurrentDate()
{
    return Content(DateTime.Now.ToString("U"));
}


来源:https://stackoverflow.com/questions/1819490/calling-asp-net-mvc-controller-explicitly-via-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!