如何使用实体框架通过读/写操作和视图实现对“Controller/”的分页?向导生成的代码?
这是由与 EF 4.1 工具打包的“控制器/具有读/写操作和视图,使用实体框架”向导生成的 ASP.MVC 3 控制器代码。当您有 10-20 条记录时,它在演示中效果很好,但是当您从具有 10,000 多条记录的 DbContext 中提取数据时,它显然会减慢速度并产生不可用的长页面。
那么,如何修改 Index() 来实现 db.Courses.ToList() 中的分页呢?
// wizard generated MVC Controller code
namespace Scheduler.Controllers
{
public class CourseController : Controller
{
private CourseEntities db = new CourseEntities();
//
// GET: /Course/
public ViewResult Index()
{
// returns over 10,000 courses in the list
return View(db.Courses.ToList());
}
}
}
This is ASP.MVC 3 controller code generated by the "Controller/ with read/write actions and views, using Entity Framework" Wizard packaged with the EF 4.1 tooling. It works great in demos when you have 10-20 records, but when you pull data from a DbContext with 10,000+ records, it obviously slows way down and produces an unusably long page.
So, how would you modify Index() to implement paging in the db.Courses.ToList()?
// wizard generated MVC Controller code
namespace Scheduler.Controllers
{
public class CourseController : Controller
{
private CourseEntities db = new CourseEntities();
//
// GET: /Course/
public ViewResult Index()
{
// returns over 10,000 courses in the list
return View(db.Courses.ToList());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 Linq to Entities 有点合适。
这是我修改后的 Index() 控制器:
然后在视图中构造链接,将页面参数附加到 url,如下所示:
/Course?page=2
Seems a little Linq to Entities is in order.
Here's my modified Index() controller:
Then construct links in the View that append the page parameter to the url like this:
/Course?page=2
您可以使用 PagedList NuGet 包。请参阅本教程:
http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application< /a>
You can use the PagedList NuGet package. See this tutorial:
http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application