ASP.Net MVC加载进程指示器 [英] ASP.Net MVC Loading In Progress Indicator

查看:125
本文介绍了ASP.Net MVC加载进程指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有经历一个过程运行的MVC控制器,设置了一些查看数据,然后在查看该数据的结果返回给用户。处理时间是依赖于正在被处理的数据量。我需要一个很好的方式来显示,而进程正在运行,让用户知道,有些事情正在发生视图内的.gif动画。

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 on the amount of data that is being processed. I need a good way to display an animated .gif within the View while the process is running, letting the user know that something is going on.

我已经看了各种AJAX方法和局部视图,但仍然找不到做到这一点的好办法。我真的希望我所能做的就是有一个ActionFilter,将显示该动画的.gif注意OnActionExecuting活动期间返回一个视图或局部视图,那么一旦控制器完成processsing并返回ViewData的,视图或局部视图与实际查看可以显示的数据。

I've looked at various AJAX methods and partial views but still cannot find a good way to accomplish this. What I really wished I could do was have an ActionFilter that would return a View or Partial View during the OnActionExecuting event that displays this animated .gif, then once the Controller completed processsing and returned the ViewData, the view or partial view with the actual View Data could be displayed.

它似乎也像jQuery将能够提供一个很好的异步方式来调用后台控制器的操作,然后显示视图。任何帮助将是AP preciated。

It also seems like jQuery would be able to provide a nice asynchronous way to call the controller action in the background and then render the View. Any help would be appreciated.

推荐答案

在你的控制器:

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

在视图(JavaScript中,使用JQuery):

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()
    });
});

请注意,这仅仅是假设了ID来操作后的路由。在行动的'X'参数是打败积极的缓存在IE中。

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

在视图(标记):

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

获取装载机这里的GIF

Get loader gifs here.

另外请注意,您不必使用JSON做到这一点。您可以从控制器动作获取其他像HTML或XML,如果你preFER。

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.

这篇关于ASP.Net MVC加载进程指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆