将大量数据从数据库加载到pagedList

时间:2016-06-22 11:11:43

标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller

从一开始我就想为虚拟问题道歉。我是MVC的新手。

当我尝试从数据库加载所有记录时,我的索引页面上的等待操作超时。

我正在使用带有搜索字段和明确选择功能的PagedList。

我的域名模型如下所示

public class BulbBatch
{
    public int BulbBatchID { get; set; }
    [Required]
    public int DeliveryID { get; set; }
    public virtual Delivery Delivery { get; set; }
    [Required]
    public int BulbsAmount { get; set; }
    public int? BoxesAmount { get; set; }
    [DataType(DataType.Date)]
    public DateTime? CoolingDate { get; set; }
    public int? BatchLocationID { get; set; }
    public virtual Location Location { get; set; }
    public string Comments { get; set; }
    public string BulbBatchBarCode { get; set; }
    public byte[] BulbBatchBarCodeImage { get; set; }
    public string BulbBatchImageURL { get; set; }
}

我的ViewModel for Index页面是下一个

public class BulbBatchViewModel
{
    public int BulbBatchID { get; set; }
    [DisplayName("Bulb Type Name")]
    public string BulbTypeName { get; set; }
    [DisplayName("Bulbs Amount")]
    public int BulbsAmount { get; set; }
    [DisplayName("Location Name")]
    public string LocationName { get; set; }
}

因为我需要使用PagedList,所以我创建了额外的viewmodel来实现它。         使用PagedList;

public class BulbBatchListViewModel
{
    public int? Page { get; set; }
    /// <summary>
    /// fields for searching
    /// </summary>
    public string BulbName { get; set; }
    public string BarCode { get; set; }
    public string LocationName { get; set; }
    /// <summary>
    /// using IPageList instead of IEnumerable to create pagination on the view
    /// </summary>
    public IPagedList<BulbBatchViewModel> SearchResult { get; set; }
    public string SearchButton { get; set; }
    public string ClearButton { get; set; }
}

最后,我的控制器中有一个Action Result Index方法,用于从数据库中检索数据并初始化我的BulbBatchesListViewModel。 我还尝试使用多个Include语句实现Eager Loading以减少对数据库的请求量

public ActionResult Index(BulbBatchListViewModel model)
{
        db.Configuration.ProxyCreationEnabled = false;
        // creating list with My viewModel for one item of BulbBatc because I need to replace indexes and add extra data from related tables
        List<BulbBatchViewModel> batchesListVM = new List<BulbBatchViewModel>();
        IQueryable<BulbBatch> results;
        string barcode = "";
        if (!string.IsNullOrEmpty(model.BarCode) || model.Page.HasValue)
            barcode = '*' + model.BarCode + '*';
        if (!string.IsNullOrEmpty(model.SearchButton) || model.Page.HasValue)
        {
            //get all records from database based upon filters                
            results = db.BulbBatches.Include(d => d.Delivery).Include(t => t.Delivery.BulbType).Where(b =>
                                                                    (b.BulbBatchBarCode.Equals(model.BarCode) || string.IsNullOrEmpty(model.BarCode)) &&
                                                                    (b.Delivery.BulbType.BulbName.ToLower().Equals(model.BulbName) || string.IsNullOrEmpty((model.BulbName))) &&
                                                                    (b.Location.LocationName.ToLower().Equals(model.LocationName.ToLower()) || string.IsNullOrEmpty(model.LocationName))
                                                                  );
            //initializing of view for each item in result 
            foreach (var item in results)
            {
                var batchVM = new BulbBatchViewModel
                {
                    BulbsAmount = item.BulbsAmount,
                    BulbTypeName = item.Delivery.BulbType.BulbName,
                    LocationName = item.Location.LocationName,
                };
                batchesListVM.Add(batchVM);
            }
            var pageIndex = model.Page ?? 1;
            model.SearchResult = batchesListVM.ToPagedList(pageIndex, RecordsPerPage);
        }
        else
        {
            //get all records from database without filtering filters
            results = db.BulbBatches.Include(d => d.Delivery).Include(t => t.Delivery.BulbType);
            foreach (var item in results)
            {
                var batchVM = new BulbBatchViewModel
                {
                    BulbsAmount = item.BulbsAmount,
                    BulbTypeName = item.Delivery.BulbType.BulbName,
                    LocationName = item.Location.LocationName,
                };
                batchesListVM.Add(batchVM);
            }
            var pageIndex = model.Page ?? 1;
            model.SearchResult = batchesListVM.ToPagedList(pageIndex, RecordsPerPage);
        }
        if (!string.IsNullOrEmpty(model.ClearButton))
        {
            model.BarCode = "";
            model.BulbName = "";
            model.LocationName = "";
            ModelState.Clear();
        }
        return View(model);
    }

当我第一次去索引页面时(没有任何过滤/搜索)我有一个错误“等待操作超时”

results = db.BulbBatches.Include(d => d.Delivery).Include(t => t.Delivery.BulbType);
                foreach (var item in results)
                {
                    var batchVM = new BulbBatchViewModel 

...

这是我的其他声明的一部分

当我尝试从数据库加载10行时,它可以正常工作,但是当我放入1000时,它会停止。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

问题解决了。 据我所知,问题是由字段引起的

public byte[] BulbBatchBarCodeImage { get; set; } 

在我的模型中。所以我做了什么,我改变了选择代码,我不再加载BarCodeImage字段了。 除了我重建一点动作

            db.Configuration.ProxyCreationEnabled = false;
        List<BulbBatchViewModel> results = new List<BulbBatchViewModel>();
        string barcode = "";
        if (!string.IsNullOrEmpty(model.BarCode))
            barcode = '*' + model.BarCode + '*';
        results = db.BulbBatches.Include(d => d.Delivery).Include(t => t.Delivery.BulbType).Include(l => l.Location)
                                .Where(b =>
                                    (b.BulbBatchBarCode.Equals(model.BarCode) || string.IsNullOrEmpty(model.BarCode)) &&
                                    (b.Delivery.BulbType.BulbName.ToLower().Equals(model.BulbName.ToLower()) || string.IsNullOrEmpty((model.BulbName))) &&
                                    (b.Location.LocationName.ToLower().Equals(model.LocationName.ToLower()) || string.IsNullOrEmpty(model.LocationName))
                                )
                                .Select(x => new BulbBatchViewModel
                                {
                                    BulbBatchID = x.BulbBatchID,
                                    BulbsAmount = x.BulbsAmount,
                                    BulbTypeName = x.Delivery.BulbType.BulbName,
                                    LocationName = x.Location.LocationName
                                })
                                .ToList();
        model.SearchResult = results.OrderBy(r => r.BulbBatchID).Skip((model.Page - 1) * model.PageSize).Take(model.PageSize).ToList();
        model.TotalRecords = db.BulbBatches.Count(b =>
                                    (b.BulbBatchBarCode.Equals(model.BarCode) || string.IsNullOrEmpty(model.BarCode)) &&
                                    (b.Delivery.BulbType.BulbName.ToLower().Equals(model.BulbName.ToLower()) || string.IsNullOrEmpty((model.BulbName))) &&
                                    (b.Location.LocationName.ToLower().Equals(model.LocationName.ToLower()) || string.IsNullOrEmpty(model.LocationName))
                                );
        return View(model);

在我的BulbBatchListViewModel中,我不再使用PagedList而是使用简单的List。在.Take()和.Skip方法的帮助下,我正在桌面上导航。

所以对于会遇到同样困难的人。 更新了ViewModel

    public class BulbBatchListViewModel
{
    public int Page { get; set; }
    public int PageSize { get; set; }
    public int TotalRecords { get; set; }
    ///<summary>
    /// order field
    /// </summary>
    public string Sort { get; set; }
    public string SortDir { get; set; }
    /// <summary>
    /// fields for searching
    /// </summary>
    public string BulbName { get; set; }
    public string BarCode { get; set; }
    public string LocationName { get; set; }
    /// <summary>
    /// we are using IPageList instead of IEnumerable to create pagination on the view
    /// </summary>
    //public IPagedList<BulbBatchViewModel> SearchResult { get; set; }
    public List<BulbBatchViewModel> SearchResult { get; set; }
    public string SearchButton { get; set; }
    public string ClearButton { get; set; }

    public BulbBatchListViewModel()
    {
        Page = 1;
        PageSize = 5;
        Sort = "BulbBatchID";
        SortDir = "ASC";
    }
}

控制器

        public ActionResult Index(BulbBatchListViewModel model)
    {
        db.Configuration.ProxyCreationEnabled = false;
        List<BulbBatchViewModel> results = new List<BulbBatchViewModel>();
        string barcode = "";
        if (!string.IsNullOrEmpty(model.BarCode))
            barcode = '*' + model.BarCode + '*';
        results = db.BulbBatches.Include(d => d.Delivery).Include(t => t.Delivery.BulbType).Include(l => l.Location)
                                .Where(b =>
                                    (b.BulbBatchBarCode.Equals(model.BarCode) || string.IsNullOrEmpty(model.BarCode)) &&
                                    (b.Delivery.BulbType.BulbName.ToLower().Equals(model.BulbName.ToLower()) || string.IsNullOrEmpty((model.BulbName))) &&
                                    (b.Location.LocationName.ToLower().Equals(model.LocationName.ToLower()) || string.IsNullOrEmpty(model.LocationName))
                                )
                                .Select(x => new BulbBatchViewModel
                                {
                                    BulbBatchID = x.BulbBatchID,
                                    BulbsAmount = x.BulbsAmount,
                                    BulbTypeName = x.Delivery.BulbType.BulbName,
                                    LocationName = x.Location.LocationName
                                })
                                .ToList();
        model.SearchResult = results.OrderBy(r => r.BulbBatchID).Skip((model.Page - 1) * model.PageSize).Take(model.PageSize).ToList();
        model.TotalRecords = db.BulbBatches.Count(b =>
                                    (b.BulbBatchBarCode.Equals(model.BarCode) || string.IsNullOrEmpty(model.BarCode)) &&
                                    (b.Delivery.BulbType.BulbName.ToLower().Equals(model.BulbName.ToLower()) || string.IsNullOrEmpty((model.BulbName))) &&
                                    (b.Location.LocationName.ToLower().Equals(model.LocationName.ToLower()) || string.IsNullOrEmpty(model.LocationName))
                                );
        return View(model);
    }

感谢你们所有人。