ASP.Net MVC Loading In Progress Indicator

為{幸葍}努か 提交于 2019-12-02 14:32:44

In your controller:

public JsonResult GetSomething(int id)
{
    return Json(service.GetSomething(id));
}

In the view (javascript, using JQuery):

$('#someLink').click(function()
{
    var action = '<%=Html.ResolveUrl("~/MyController.mvc/GetSomething/")%>' + $('#someId').val() + '?x=' + new Date().getTime();
    $('#loading').show()
    $.getJSON(action, null, function(something) 
    {
        do stuff with something
        $('#loading').hide()
    });
});

Note that this assumes a route where 'id' comes after action. The 'x' parameter on the action is to defeat aggressive caching in IE.

In the view (markup):

<img id="loading" src="images/ajax-loader.gif" alt=""/> 
<!-- use a css stlye to make display:none -->

Get loader gifs here.

Also note that you do not have to do this with Json. You can fetch other things like HTML or XML from the controller action if you prefer.

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