简体   繁体   English

使用数据表 C# MVC 减慢页面加载速度

[英]Page loading to slow using datatables C# MVC

I am trying to clean up some pages in my solution.我正在尝试清理解决方案中的一些页面。 The pages or tables load slower than i would like.页面或表格的加载速度比我想要的慢。 I am using a view model and doing a Foreach on the page in a table and then using datatables to display.我正在使用视图模型并在表中的页面上执行 Foreach,然后使用数据表进行显示。 It seems that on the database side it loads each record 1 at a time and with 1000s of records it takes way to long to load.似乎在数据库端,它一次加载每条记录 1 条记录,加载 1000 条记录需要很长时间。 When i run the query in SQL it is instantly populated.当我在 SQL 中运行查询时,它会立即填充。 I am looking for some help to clean the code up and see if there is anything here that can be changed to make it load faster.我正在寻找一些帮助来清理代码,看看这里是否有任何可以更改以使其加载速度更快的内容。 Since I am using the foreach in the page the DeferRender does not work.由于我在页面中使用了 foreach,因此 DeferRender 不起作用。 It does not matter to me if i use this or JavaScript, certainly server side coding is an option.如果我使用这个或 JavaScript 对我来说无关紧要,当然服务器端编码是一种选择。 In the controller the use of an array may work also but i do not know how to incorporate that with a view model.在控制器中,数组的使用也可能有效,但我不知道如何将其与视图模型合并。 "db.Companys.ToArray();" “db.Companys.ToArray();” works for pulling from the model but the ToArray doesn't work with a viewModel to my knowledge.适用于从模型中提取,但据我所知,ToArray 不适用于 viewModel。

Below is my code.下面是我的代码。

Controller:控制器:

public ActionResult Index()
    {
        var data = (from temp in db.TopLvlJobs
                    join serial in db.SerialNumbers on temp.TopLvlJob equals serial.TopLvlJob
                    select new TopLvlJobsViewModels
                    {
                        PartNumber = temp.PartNumber,
                        Description = temp.Description,
                        ExtDescription = temp.ExtDescription,
                        Drawing = temp.Drawing,
                        IsDelete = false,
                        CreatedDate = DateTime.UtcNow,
                        UpgradeAvailable = temp.UpgradeAvailable,
                        TopLvlJob = temp.TopLvlJob,
                        SerialNumber = serial.SerialNumber

                    }).OrderBy(x => x.CreatedDate);

        return View(data);
    }

ViewModel:视图模型:

public class TopLvlJobsViewModels
{
    [Key]
    [Display(Name = "TopLvlJob")]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string TopLvlJob { get; set; }
    [Display(Name = "Description")]
    [Required]
    public string Description { get; set; }
    [Display(Name = "Ext Description")]
    [Required]
    public string ExtDescription { get; set; }
    [Display(Name = "Part Number")]
    [Required]
    public string PartNumber { get; set; }
    [Display(Name = "Drawing")]
    [Required]
    public string Drawing { get; set; }
    [UIHint("_IsStatus")]
    [Display(Name = "Upgrade Available ?")]
    [Required]
    public bool UpgradeAvailable { get; set; }

    public DateTime? CreatedDate { get; set; }

    public string CreatedBy { get; set; }

    public DateTime? UpdatedDate { get; set; }

    public string UpdatedBy { get; set; }

    public bool? IsDelete { get; set; }
    [Display(Name = "Serial")]
    public string SerialNumber { get; set; }
    public TopLvlJobs topsjobs { get; set; }

    public HttpPostedFileBase Pdfupload { get; set; }

    public virtual IEnumerable<SubJobs> SubJobs { get; set; }
    public virtual IEnumerable<Upgrades> Upgrade { get; set; }
}

JavaScript on .CHTML page .CHTML 页面上的 JavaScript

    $(document).ready(function () {
    //var oTableMenuPermission = "";
    //oTableMenuPermission = 
    $("#SubJobsListTable").dataTable({
        "bRetrieve": true,
        "bProcessing": true,
        "dom": 'lBfrtip',
        "buttons": [
            { extend: 'copyHtml5', exportOptions: { columns: ':visible' } }
            , { extend: 'excelHtml5', exportOptions: { columns: ':visible' } }
            , { extend: 'csvHtml5', exportOptions: { columns: ':visible' } }
            , { extend: 'pdfHtml5', exportOptions: { columns: ':visible' } }
            , { extend: 'print', exportOptions: { columns: ':visible' } }
            , 'colvis'
        ], columnDefs: [{ visible: false, targets: [3] }, { orderable: false, targets: [2, 3, 4, 5, 6, 7, 8] }],
        "pageLength": 10,
        "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
    });
    //table.buttons().container()
    //    .appendTo($('.col-lg-8:eq(0)', table.table().container()));
});

I can give you anything else needed, I didn't post the whole CHTML page due to length, however it is just a basic Foreach in a table View.我可以给你任何其他需要的东西,由于篇幅原因,我没有发布整个 CHTML 页面,但它只是表视图中的一个基本 Foreach。

Thanks for your help!谢谢你的帮助!

use IQueryable instead of IEnumerable and try使用 IQueryable 而不是 IEnumerable 并尝试

Querying data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data.从数据库查询数据,IEnumerable 在服务器端执行选择查询,在客户端加载内存中的数据,然后过滤数据。 Querying data from a database, IQueryable execute the select query on the server side with all filters.从数据库查询数据,IQueryable 使用所有过滤器在服务器端执行选择查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM