在服务器端处理中两次加载实体框架,数据表抛出错误

时间:2018-09-26 08:24:51

标签: c# asp.net-mvc datatable datatables

我当前正在使用带有MVC core2.0的数据库来构建我的应用程序,但是,当我尝试在服务器代码中两次访问数据库时,浏览器将向我抛出错误,提示无法加载资源:net :: ERR_SPDY_PROTOCOL_ERROR

以下是我在js中的数据表设置

 settingsInitEmpty = {
            ajax: ({
                url: "/Batch/LoadWithdrawalListData?batch_id=@Context.Request.Query["batchID"].ToString()&search_type=" + search_type + "&value=" + value + "&status=" + status + "&agent=" + agent,
                type: "POST",
                datatype: "json"
            }),
            // ### FOR SERVERSIDE LOADING
            processing: true,
            serverSide: true,
            filter: false,
            orderMulti: false,
            order: [[2, "desc"]],
            columnDefs: [
                { targets: 0, className: 'text-center', orderable: false },
                { targets: 1, className: 'text-center', orderable: false },
                { targets: 2, className: 'text-left' },
                { targets: 3, className: 'text-center' },
                { targets: 4, className: 'text-right' },
                { targets: 5, className: 'text-right' },
                { targets: 6, className: 'text-right' },
                { targets: 7, className: 'text-right' },
                { targets: 8, className: 'text-center' },
                { targets: 9, className: 'text-left' },
                { targets: 10, className: 'text-center' },
                { targets: 11, className: 'text-center'  },
                { targets: 12, className: 'text-center', orderable: false },
            ],
            columns: [
                { data: "id", name: "id", autoWidth: true },
                { data: "name", name: "name", autoWidth: true },
                { data: "amount", name: "amount", autoWidth: true, render: $.fn.dataTable.render.number(',', '.', 2, 'US$ ') },
                { data: "xchangeRate", name: "xchangeRate", autoWidth: true },
                { data: "processingFee", name: "processingFee", autoWidth: true, render: function (data, type, row) { if (row.processingFee != null) { return '¥ ' + row.processingFee; }else return '' } },

在服务器端,以下代码是我的代码

  var userAgentList =  _userManager.Users.Where(x=>x.Role == "Agent");
            var draw = Request.Form["draw"].FirstOrDefault();
            var start = Request.Form["start"].FirstOrDefault();
            var length = Request.Form["length"].FirstOrDefault();
            //Find Order Column
            var sortColumn = Request.Form["columns[" + Request.Form["order[0][column]"].FirstOrDefault() + "][name]"].FirstOrDefault();
            var sortColumnDir = Request.Form["order[0][dir]"].FirstOrDefault();

            //var search_type = HttpUtility.UrlDecode(Request.Query["search_type"]);
            //var value = HttpUtility.UrlDecode(Request.Query["value"]);
            var batch_id = HttpUtility.UrlDecode(Request.Query["batch_id"]);
            var search_type = HttpUtility.UrlDecode(Request.Query["search_type"]);
            var value = HttpUtility.UrlDecode(Request.Query["value"]);
            var status = HttpUtility.UrlDecode(Request.Query["status"]);
            var agent =HttpUtility.UrlDecode(Request.Query["agent"]);


            int pageSize = length != null ? Convert.ToInt32(length) : 0;
            int skip = start != null ? Convert.ToInt32(start) : 0;
            int recordsTotal = 0;

            IQueryable<WithdrawalRecords> withdrawalList = _context.WithdrawalRecords;
            //query = query.Where(p => p.product_name.Contains(pageModel.searchList.product_name));

            withdrawalList = withdrawalList.Where(p => p.BatchId.Equals(batch_id));


            //SORT
            if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
            {
                withdrawalList = withdrawalList.OrderBy(sortColumn + " " + sortColumnDir);
            }
            recordsTotal = withdrawalList.Count();
            var data2 = withdrawalList.Skip(skip).Take(pageSize).ToList();

            List<string> serial = new List<string>();
            for (int a = 0; a < data2.Count(); a++)
            {
                if (!String.IsNullOrWhiteSpace(data2[a].SubBatchId))
                {
                    serial.Add(data2[a].SubBatchId);
                }
            }
            _context.SaveChanges();
            var subBatchList = _context.SubBatch.Where(x => serial.Contains(x.Id)).ToList();
            for (int a = 0; a < subBatchList.Count(); a++)
            {
                var agentName = userAgentList.Where(x => x.Id == subBatchList[a].AgentId).Single().Name;
                for (int b = 0; b < data2.Count(); b++)
                {
                    if (data2[b].SubBatchId == subBatchList[a].Id)
                    {
                        data2[b].Comment = agentName;
                    }
                }
            }


            var result = Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data2 });
            return result;

好的,现在的问题是,在构建和调试代码时,没有错误,或者我的控制器抛出了任何异常,因此,以下结果已返回,但未显示任何结果。

enter image description here

请查看我的c#代码,如果我注释掉第二个从数据库获取数据的tolist()函数,整个函数就可以了,该错误仅在我在c#函数中具有2个tolist函数时才会发生

0 个答案:

没有答案