ASP.NET MVC Webgrid高效分页 [英] ASP.NET MVC Webgrid Efficient Paging
问题描述
我有一个ASP.NET MVC 4项目和一个SQL视图(vvItem). ItemController
I have a ASP.NET MVC 4 project and a SQL View (vvItem). ItemController
MVCAppEntities db = new MVCAppEntities();
public ActionResult Index()
{
var itemqry = db.vvItem.OrderBy(s => s.name);
//var pageditems = itemqry.Skip(10).Take(20); // 25 seconds
return View(itemqry.ToList()); // 88 seconds
}
Index.cshtml视图
Index.cshtml View
@model IEnumerable<MVCApplication1.Models.vvItem>
@{
var norows = 20;
var grid = new WebGrid(Model, canPage: true, rowsPerPage: norows);
grid.Pager(WebGridPagerModes.NextPrevious);
@grid.GetHtml(tableStyle: "table", headerStyle: "header", columns: grid.Columns(
grid.Column(columnName: "name", header: "Name", canSort: true),
grid.Column(columnName: "quantity", header: "Quantity", canSort: true),
grid.Column(columnName: "code", header: "Code", canSort: true),
grid.Column(columnName: "Price", header: "Price", canSort: true),
))}
在vvItem中,我几乎有400000条记录.我以为Webgrid Pager只会加载(Take())显示的记录,并且如果我转到下一页,它将知道Skip()第一条记录.
In vvItem I have almost 400000 records. I thought that the webgrid Pager would load (Take()) only the displayed records and it would know to Skip() the first records if I would go to the next pages.
问:如何有效地制作视图以仅加载显示的记录?
Q : How can I efficiently make a view to load only the displayed records ?
我找到了2个解决方案: JSON版本和
I found 2 solutions : JSON version and NerdDinner
我不太擅长JSON,因此尝试了NerdDinner解决方案.和我的评论行一样
//var pageditems = itemqry.Skip(10).Take(20);
itemqry已经加载了所有记录,并且花费了很多时间.
I'm not so good at JSON so I tried the NerdDinner solution. And as in my commented line
//var pageditems = itemqry.Skip(10).Take(20);
itemqry is already loaded with all the records and it took a lot of time to load.
第二季度:我现在如何进行分页?我需要修改页面编号.通过索引方法.
Q2 : How can I do paging now ? I need to modify the page no. from the Index method.
public ActionResult Index(int? page, string filter1 = " ", string filter2 = " ")
推荐答案
我做了一个SQL存储过程
I made a SQL Stored Procedure
CREATE PROCEDURE SkipTake
@pagNo int,
@pageSize int
AS
select *
from (select *, row_number() over (order by COD) as rn
from vvSTOC
) as T
where T.rn between (@pagNo - 1) * @pageSize + 1 and @pagNo * @pageSize
我已在函数导入"的EF模型中添加了此sp,以便它返回实体(vvSTOC)
I've added this sp in my EF model at Function Import so that it returns an entity (vvSTOC)
public ActionResult Index(int? page)
{
const int pageSize = 20;
return View(db.spSkipTake(page, pageSize).ToList());
}
这篇关于ASP.NET MVC Webgrid高效分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!