数据更改时如何自动刷新局部视图 [英] How to automatically refresh a partial view when data changes

查看:72
本文介绍了数据更改时如何自动刷新局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 3应用程序(A),该应用程序允许多个客户端处理数据表格-例如调查表格.有一个外部过程(B),向其发送调查表.然后,外部流程(B)找出有多少客户正在处理这些调查表(可能有数千个调查),并向每个客户分发尽可能多的当前调查集(每个客户可以持有固定数量的调查)问卷调查),这样当客户登录到主应用程序(A)时,他将登录到处理应用程序,并且将向该客户显示未处理表格的子集,然后他/她可以选择处理该表格.当客户从列表中选择一个或多个要处理的项目时,这些项目将从列表中删除.在任何给定时间可能没有足够的客户端来完成所有未完成的未处理调查,但是外部过程会在每个刷新间隔(例如15分钟)内将调查重新分配给当前已登录客户端集.

现在,假设有更多的客户登录以使其中的一些项目得以执行.这会导致流程(B)将未处理的调查表重新分配给所有当前活动(登录)的客户.
问题:

当客户登录时,他将看到他可以进行的调查的列表.但是,如果他在登录后的15分钟内未处理所有显示的调查,则外部过程(B)将重新分配调查,以使他当前在屏幕上看到的内容不再准确.他要做的就是刷新页面,他将获得最新的页面.但是我希望他不必刷新,而我想让外部过程(B)发送一个事件,该事件将导致显示客户调查列表的部分视图随时间更新.最新视图.

我一直在寻找实现此目的的方法,但到目前为止还没有提出任何想法.

需要更新的数据在局部视图中,因此,如果我可以订阅某种事件,那么当进程(B)拥有新数据时,它可能会触发该事件并且可以将其拾取.

在这种情况下,整个应用程序都是Intranet应用程序,因此服务和客户端位于同一建筑物中,并且都在防火墙内部.

我想做的是这样的:

I have an MVC 3 application (A) which allows multiple clients to process data forms - for example a survey form. There is an external process (B) to which the survey forms are sent. The external process (B) then figures out how many clients are working to process these survey forms (there may be thousands of surveys) and distributes as many of the the current set of surveys to each client (where each client can hold a fixed number of surveys) such that when the client logs into the main application (A) he will log into the processing application and that client will be shown a subset of the unprocessed forms which he/she can then select to work on. When the client has chosen one or more items to work on from the list, those items are removed from the list. There may not be enough clients at any given time to fullfill all of the outstanding unprocessed surveys, but the external process re-distributes the surveys to the current set of logged on clients every refresh interval (say 15 minutes.)

Now, say more clients log on to get some of these items to work on; this causes the Process (B) to redistribute the unprocessed survey forms to all current active (logged in) clients.
The problem:

when the client logs in he will see a list of surveys that he can work on. However, if he does not process all of the displayed surveys in the 15 minute period following his logging in, then the external process (B) will have redistributed the surveys so that what he currently sees on his screen my no longer be accurate. All he has to do is to refresh his page and he''ll get the latest set. But I''d like him to not have to refresh but rather I''d like to have the external process (B) send an event that would cause the partial view which shows the client''s list of surveys to update with the latest view.

I have been searching for a way to do this but so far haven''t come up with any ideas.

The data that needs updated is in a partial view so if I could subscribe to some kind of event then when the process (B) had new data it could fire this event and I could pick it up.

In this situation the entire application is an intranet application so the service and the clients are in the same building and are all inside the firewall.

What I would like to be able to do is something like this:

 public myController
{
   public ActionResult Index()
   {
        // get list of my surveys
        var results = MyRepository.GetNewSurveys();

        // add to the view model
        var viewModel = new ViewModel() { Results = results};

        // somehow subscribe to some kind of event

        return View(viewModel);
        // return a view
   }

   public void SomeEventHandler()
   {
         // update partial view that is inside the Index View's cshtml with the updated list
   }



Index.cshtml在这里...



Index.cshtml here...

@using ViewModel
<div>
   <h2>My View</h2>
   <p> some data here </p>
   <div id="mypartialView">
   @{html.Renderpartial("_results", Model.Results);}
   </div>
</div>




在此先谢谢您




Thanks in advance

推荐答案

您可以从以下类似内容开始: http://geekswithblogs .net/blachniet/archive/2011/08/03/updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx [ http://html5doctor. com/server-sent-events/ [^ ]

祝你好运!
You can start with something like this one: http://mazharkaunain.blogspot.hu/2011/05/aspnet-mvc-razor-render-partial-view.html[^], or this one: http://geekswithblogs.net/blachniet/archive/2011/08/03/updating-partial-views-with-unobtrusive-ajax-in-mvc-3.aspx[^].

I would add a little ajax callback in the page. The backend would check if the data is changed, and if it was changed, it would return a specific message, that can be handled by the page to reload the partial view. This callback could be called in every x seconds. But be aware, this might be a heavy load if there are many clients, thus you have to guess this interval properly. I would also play around with http 1.1 keep-alive also on server side.

I know of no other all-around supported way of having the server send out events to the browser. In html5 you have an api for that, but I don''t know about it''s support: http://html5doctor.com/server-sent-events/[^]

Good luck!


这篇关于数据更改时如何自动刷新局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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