asp.net mvc 3 webgrid绑定到列表<动态>是非常缓慢 [英] asp.net mvc 3 webgrid bound to List<dynamic> is exceedingly slow

查看:127
本文介绍了asp.net mvc 3 webgrid绑定到列表<动态>是非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个可以在视图中返回可变数量的列的数据表,因此我将Mvc 3 WebGrid绑定到列表< dynamic>,如本文的答案所述:从DataTable播放MVC Webgrid

I need to display a data table that can return a variable number of columns in a view, so I am binding an Mvc 3 WebGrid to a List<dynamic>, as described in the answer to this post: Populate MVC Webgrid from DataTable

它的作品好,但是是非常慢的! 令人难以置信的慢我的意思是需要13秒显示一组15个记录与11列。有什么办法加速吗?我尝试删除Pager,但没有任何效果。

It works fine, but is incredibly slow! By "incredibly slow" I mean it takes 13 seconds to display a set of 15 records with 11 columns. Is there any way to speed this up? I've tried removing the Pager but that has no effect.

创建列表<动态>从Ado.Net数据表看起来像这样。它运行得很快,没有问题。

The code that creates the List<dynamic> from an Ado.Net data table looks like this. It runs very quickly, no problem here.

var MyList = new List<dynamic>();
foreach (DataRow row in MyTable.Rows)
{
   var expando = (IDictionary<string, object>)new ExpandoObject();
   foreach (string column in columnNames)
   {
       expando.Add(column, row[column]);
   }
   MyList.Add(expando);
}

视图中出现问题。下面的代码需要大约13秒才能渲染一组包含11列的15条记录!数据库的访问和数据表的转换为List< dynamic>花费不到一秒钟。下面的代码需要13秒,只是渲染。我究竟做错了什么?或者我只是使用列表<动态>?b ing了错误的树?

The problem occurs in the view. The code below takes about 13 seconds to render a set of 15 records with 11 columns! The trip to the database and the conversion of the data table to a List<dynamic> takes less than a second. The code below takes 13 seconds, just to render. What am I doing wrong? Or am I just barking up the wrong tree using a List<dynamic>?

var grid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: false);
 grid.Bind(Model.MyList, rowCount: (int)Model.RowCount, autoSortAndPage: false);
 grid.Pager(WebGridPagerModes.All);
                @grid.GetHtml(tableStyle: "webgrid",
                    rowStyle: "webgrid-row",
                    alternatingRowStyle: "webgrid-alternating-row",
                         htmlAttributes: new { id = "tblSearchResults" },
                         firstText: "<<First",
                         lastText: "Last>>", mode: WebGridPagerModes.All
                )


推荐答案

我有同样的问题,这是非常讨厌的,因为我们正在加载任意数据。

I had the same issue and it was very annoying because we were loading up arbitrary data.

修复是为这些列定义列和格式函数,这样可以通过一个巨大的因素来提高WebGrid的性能,并且性能可以更好地(基于列数)

The fix is to define columns and a format function for those columns, this improves the WebGrid performance by a HUGE factor and the performance scales far better (based on number of columns) than without format defined.

例如

IEnumerable<KeyValuePair<string, Object>> example = data.First();
foreach (var pair in example)
    cols.Add(grid.Column(
        columnName: pair.Key,
        //Seems that specifying a formatter saves the web grid ridiculous amounts of time
        format: (item => ((IDictionary<String, Object>)item.Value)[pair.Key])
    ));

查看我的小例子项目 https://github.com/jamesk/MVCWebGridSpeedTest 进行速度比较和完整示例代码。

See my little example project https://github.com/jamesk/MVCWebGridSpeedTest for a speed comparison and full example code.

这篇关于asp.net mvc 3 webgrid绑定到列表&lt;动态&gt;是非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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