ASP.NET MVC-懒加载-逐步加载数据库信息

环境:

win10, .NET 6.0


目录

问题描述

假设我数据库中有N个表,当我打开某页面时,每个表都先加载一部分(比如20条),点击表下某个按钮,再加载下一部分,如此循环直至加载完毕。

解决方案

基础版

数据库查询部分(Entity Framework)

  1. BasicPartsDbContext.cs
csharp 复制代码
using System.Data.Entity;

namespace WebApplication1.Models
{
    public class BasicPartsDbContext:DbContext
    {
        public BasicPartsDbContext() : base("name=conn1") { }
        public DbSet<BasicParts> BasicParts { get; set; }
    }
}

其中BasicParts是我的实体/模型类,数据类型与数据库中某个表一一对应,内容大概如下:

csharp 复制代码
using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication1.Models
{
    [Table("dbo.表名")]
    public class BasicParts
    {
        // 对应列
    }
}

"name=conn1"是指使用此数据库配置。该配置在项目根目录下的Web.config中:

  1. BasicPartsRepository.cs
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class BasicPartsRepository
    {
        private BasicPartsDbContext _context;

        public BasicPartsRepository(BasicPartsDbContext context)
        {
            _context = context;
        }

        public List<BasicParts> GetPagedData(int pageIndex, int pageSize) {
            return _context.BasicParts.OrderBy(i => i.id)
                .Skip(pageIndex * pageSize)
                .Take(pageSize)
                .ToList();
        }
    }
}

控制器

csharp 复制代码
public class HomeController : Controller {
	private BasicPartsRepository _basicPartsRepository;
	...
	
	public ActionResult BasicPartsView() { 
    	return View();
	}

	[HttpGet]
	public JsonResult LoadMoreBasicParts(int pageIndex, int pageSize) {
    	var data = _basicPartsRepository.GetPagedData(pageIndex, pageSize);
    	return Json(data, JsonRequestBehavior.AllowGet);
	}
	...
}

前端页面

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Load More Data Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div id="data-container">
        <!-- 这里将显示从服务器加载的数据 -->

    </div>
    
    <button id="load-more">加载更多</button>

    <script>
        var pageIndex = 0;
        var pageSize = 20;

        function loadMoreData() {
            $.ajax({
                url: '/Home/LoadMoreBasicParts',
                data: {
                    pageIndex: pageIndex,
                    pageSize: pageSize
                },
                success: function (data) {
                    pageIndex++;
                    // 将新加载的数据追加到页面上
                    data.forEach(function (item) {
                        $('#data-container').append('<p>' + item.name + '</p>');
                    });
                }
            });
        }

        $(document).ready(function () {
            $('#load-more').on('click', function () {
                loadMoreData();
            });

            // 页面加载完成时,加载初始数据
            loadMoreData();
        });
    </script>
</body>
</html>

加载到表格版

其他部分保持不变,只修改前端:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Load More Data into Table</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <table id="data-table" border="1">
        <thead>
            <tr>
                <th>No.</th>
                <th>名称</th>
                <th>序列</th>
                <th>描述</th>
                <th>类型</th>
            </tr>
        </thead>
        <tbody>
            <!-- 这里是数据行 -->
        </tbody>
    </table>
    <button id="load-more">加载更多</button>

    <script>
        var pageIndex = 0;
        var pageSize = 20;

        function loadMoreData() {
            $.ajax({
                url: '/Home/LoadMoreBasicParts',
                data: {
                    pageIndex: pageIndex,
                    pageSize: pageSize
                },
                success: function (data) {
                    pageIndex++;
                    // 将新加载的数据追加到表格中
                    data.forEach(function (item) {
                        $('#data-table tbody').append(
                            '<tr>' +
                            '<td>' + item.id + '</td>' +
                            '<td>' + item.name + '</td>' +
                            '<td>' + item.seq + '</td>' +
                            '<td>' + item.info + '</td>' +
                            '<td>' + item.stype + '</td>' +
                            '</tr>'
                        );
                    });
                }
            });
        }

        $(document).ready(function () {
            $('#load-more').on('click', function () {
                loadMoreData();
            });

            // 页面加载完成时,加载初始数据
            loadMoreData();
        });
    </script>
</body>
</html>
相关推荐
小威编程1 分钟前
C# (.net6)实现Redis发布和订阅简单案例
数据库·redis·缓存
读心悦26 分钟前
MySQL,多条件分页查询语句
数据库·mysql
golitter.44 分钟前
MySQL - 索引
数据库·mysql
读心悦1 小时前
MySQL 多条件查询
android·数据库·mysql
2401_857026231 小时前
SpringBoot框架下的服装生产管理自动化
数据库·spring boot·自动化
iQM751 小时前
为什么MySQL不建议使用delete删除数据
数据库·mysql
Xerale1 小时前
Laravel Admin 中的 “Array to String Conversion“ 问题及其解决方法
前端·数据库·笔记·php·laravel
丶21362 小时前
【SQL】深入探索SQL调优:提升数据库性能的全面指南
数据库·sql·性能优化
盒马盒马2 小时前
Redis:分布式 - 哨兵
数据库·redis·分布式