ASP.Net MVC Loading In Progress Indicator

后端 未结 1 1276
北荒
北荒 2021-01-30 05:52

I have an MVC Controller that runs through a process, sets some View Data, and then returns the results of this data back to the user in a View. The process time is dependent o

相关标签:
1条回答
  • 2021-01-30 06:19

    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.

    0 讨论(0)
提交回复
热议问题