簡體   English   中英

將大量數據從數據庫加載到pagedList

[英]Load big amount of data from database to pagedList

從一開始,我想為虛擬問題道歉。 我是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; }
}

最后,我在控制器內有一個“操作結果索引”方法,用於從數據庫檢索數據並初始化我的BulbBatchesListViewModel。 我還嘗試使用多個Include語句實施Eager Load,以減少對數據庫的請求量

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 

...

這是我的else陳述的一部分

當我嘗試從數據庫加載10行時,它可以正常運行,但是當我放置1000行時,它就停止了。

請幫我解決這個問題。

問題解決了。 據我了解,問題是由領域引起的

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);
    }

感謝大家。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM