ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫信息
環(huán)境:
win10, .NET 6.0
問題描述
假設(shè)我數(shù)據(jù)庫中有N個(gè)表,當(dāng)我打開某頁面時(shí),每個(gè)表都先加載一部分(比如20條),點(diǎn)擊表下某個(gè)按鈕,再加載下一部分,如此循環(huán)直至加載完畢。
解決方案
基礎(chǔ)版
數(shù)據(jù)庫查詢部分(Entity Framework)
BasicPartsDbContext.cs
using System.Data.Entity; namespace WebApplication1.Models { public class BasicPartsDbContext:DbContext { public BasicPartsDbContext() : base("name=conn1") { } public DbSet<BasicParts> BasicParts { get; set; } } }
其中BasicParts
是我的實(shí)體/模型類,數(shù)據(jù)類型與數(shù)據(jù)庫中某個(gè)表一一對(duì)應(yīng),內(nèi)容大概如下:
using System.ComponentModel.DataAnnotations.Schema; namespace WebApplication1.Models { [Table("dbo.表名")] public class BasicParts { // 對(duì)應(yīng)列 } }
而"name=conn1"
是指使用此數(shù)據(jù)庫配置。該配置在項(xiàng)目根目錄下的Web.config中:
2. BasicPartsRepository.cs
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(); } } }
控制器
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); } ... }
前端頁面
<!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"> <!-- 這里將顯示從服務(wù)器加載的數(shù)據(jù) --> </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++; // 將新加載的數(shù)據(jù)追加到頁面上 data.forEach(function (item) { $('#data-container').append('<p>' + item.name + '</p>'); }); } }); } $(document).ready(function () { $('#load-more').on('click', function () { loadMoreData(); }); // 頁面加載完成時(shí),加載初始數(shù)據(jù) loadMoreData(); }); </script> </body> </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> <!-- 這里是數(shù)據(jù)行 --> </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++; // 將新加載的數(shù)據(jù)追加到表格中 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(); }); // 頁面加載完成時(shí),加載初始數(shù)據(jù) loadMoreData(); }); </script> </body> </html>
到此這篇關(guān)于ASP.NET MVC-懶加載-逐步加載數(shù)據(jù)庫信息的文章就介紹到這了,更多相關(guān)ASP.NET MVC逐步加載數(shù)據(jù)庫信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字
- 使用EF Code First搭建簡易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移
- asp.net mvc CodeFirst模式數(shù)據(jù)庫遷移步驟詳解
- asp.net實(shí)現(xiàn)的MVC跨數(shù)據(jù)庫多表聯(lián)合動(dòng)態(tài)條件查詢功能示例
- asp.net mvc 從數(shù)據(jù)庫中讀取圖片的實(shí)現(xiàn)代碼
- asp.net MVC 根據(jù)菜單樹類別不同動(dòng)態(tài)加載視圖的實(shí)現(xiàn)步驟
- ASP.NET?MVC使用jQuery的Load方法加載靜態(tài)頁面及注意事項(xiàng)
- ASP.NET Mvc開發(fā)之EF延遲加載
相關(guān)文章
ASP.NET Core 9.0 中新增的MapStaticAssets() 中
文章介紹了ASP.NET Core 9.0新增的MapStaticAssets中間件,該中間件解決了UseStaticFiles存在的缺陷,如缺乏靜態(tài)資源傳輸壓縮、ETag低效緩存和缺乏指紋識(shí)別,它通過生成時(shí)間壓縮、基于內(nèi)容的ETags和指紋識(shí)別來提升性能,感興趣的朋友一起看看吧2024-12-12ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法
ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法,需要的朋友可以參考一下2013-05-0512小時(shí)制和24小時(shí)制獲取當(dāng)天零點(diǎn)的問題探討
這篇文章介紹了12小時(shí)制和24小時(shí)制獲取當(dāng)天零點(diǎn)的問題探討,有需要的朋友可以參考一下2013-09-09asp.net 關(guān)于字符串內(nèi)范圍截取的一點(diǎn)方法總結(jié)
前兩天有一位網(wǎng)友提出了一個(gè)字符串內(nèi)截取字符串的問題,除了用普通的字符串截取的方式外,我推薦的是用LINQ方式來截取。兩者實(shí)際上差別不是很大,都是采用字符串截取方式,但后者從寫法和觀察效果會(huì)比前者簡單實(shí)用得多。2010-02-02