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>
相关推荐
djjjx.7 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
CoderYanger11 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
MSTcheng.12 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
Yeats_Liao13 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
鹤落晴春13 小时前
MySQL日志梳理
运维·数据库·mysql
五彩小白14 小时前
Redis的过期删除和内存淘汰机制
数据库·redis·缓存
学长毕设14 小时前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
apcipot_rain15 小时前
C++转Java日记(14)
数据库
龙腾AI白云18 小时前
数字孪生与计算机视觉:让机器看懂现场
大数据·数据库·机器学习·知识图谱·pygame
千寻技术帮19 小时前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue