MVC jQuery.Ajax非同步實作增刪改查與分頁_jquery
本文實例為大家分享了mvc jquery.ajax非同步實現增刪改查和分頁的具體程式碼,供大家參考,具體內容如下
1、model層程式碼
using system; using system.data; using system.configuration; using system.linq; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.xml.linq; using system.collections.generic; using mvcexamples; using system.web.mvc; namespace mvcexamples.web.models { public class studentmodels { /// <summary> /// 获取学生信息列表 /// </summary> public list<mvcexamples.model.student> studentlist { get; set; } /// <summary> /// 获取教工信息列表 /// </summary> public list<mvcexamples.model.teacher> teacherlist { get; set; } /// <summary> /// 获取学生信息列表(分页) /// </summary> public pagedlist<mvcexamples.model.student> getstudentlist { get; set; } } }
2、view層程式碼
<%@ page title="" language="c#" masterpagefile="~/views/shared/site.master" inherits="system.web.mvc.viewpage<mvcexamples.web.models.studentmodels>" %> <asp:content id="content1" contentplaceholderid="titlecontent" runat="server"> index </asp:content> <asp:content id="content3" contentplaceholderid="headcontent" runat="server"> <script src="http://www.cnblogs.com/scripts/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="http://www.cnblogs.com/scripts/my97datepicker/wdatepicker.js" type="text/javascript"></script> <script src="http://www.cnblogs.com/scripts/windwui/jquery-ui-1.8.1.min.js" type="text/javascript"></script> <link href="http://www.cnblogs.com/scripts/windwui/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $(function(){ //添加学生信息 $('#a_add').click(function(){ $('#window').dialog({ title: "添加学生信息", width: 300, height: 200, modal: true, buttons: { "取消": function() { $(this).dialog("close"); //当点击取消按钮时,关闭窗口 }, "添加": function() { //当点击添加按钮时,获取各参数的值 var sno=$('#sno').val(); var sname=$('#sname').val(); var ssex=$('#ssex').val(); var sbirsthday=$('#sbirthday').val(); var sclass=$('#sclass').val(); $.ajax({ type:'post', url:'/student/addstudent',//路径为添加方法 data:'no='+sno+'&name='+sname+'&sex='+ssex+'&birsthday='+sbirsthday+'&sclass='+sclass,//参数的个数和名字要和方法的名字保持一致 success:function(json)//返回的是json类型的 { $('#window').dialog("close"); //判断是否成功 if(json.result=="true") { $('#btn_close').click(); alert('恭喜你,修改成功!'); }else{ alert('抱歉,修改失败!'); } } }); } } }); }) //删除学生信息 $('.a_delete').click(function(){ //确认是否删除 if(confirm("是否删除此条信息?")) { $.ajax({ type:'post', url:'/student/deletestudent', data:'no='+$(this).attr("sno"),//获取当前对象的属性(自定义属性)sno的值,用自定义属性保存相应需要的数据 sucess:function(json) { if(json.result=="true") { alert("恭喜你,已成功删除!"); }else { alert("抱歉,删除失败!"); } } }) } }) //修改学生信息 $('.a_update').click(function(){ var student=$(this); $("#sno").attr("value",student.attr("sno")); $("#sname").attr("value",student.attr("sname")); $("#ssex").attr("value",student.attr("ssex")); $("#sbirthday").attr("value",student.attr("sbirthday")); $("#sclass").attr("value",student.attr("sclass")); $('#window').dialog({ title: "修改学生信息", width: 300, height: 200, modal: true, buttons: { "取消": function() { $(this).dialog("close"); }, "修改": function() { var sno=$('#sno').val(); var sname=$('#sname').val(); var ssex=$('#ssex').val(); var sbirsthday=$('#sbirthday').val(); var sclass=$('#sclass').val(); $.ajax({ type:'post', url:'/student/updatestudent', data:'no='+sno+'&name='+sname+'&sex='+ssex+'&birsthday='+sbirsthday+'&sclass='+sclass, success:function(json) { $('#window').dialog("close"); if(json.result=="true") { $('#btn_close').click(); alert('恭喜你,修改成功!'); }else{ alert('抱歉,修改失败!'); } } }); } } }); }); }) </script> </asp:content> <asp:content id="content2" contentplaceholderid="maincontent" runat="server"> <h2> mvc 演示</h2> <table> <thead> <tr> <td> 学生表 </td> </tr> <tr> <td> 学号 </td> <td> 姓名 </td> <td> 性别 </td> <td> 生日 </td> <td> 班级 </td> <td> 操作 </td> </tr> </thead> <tbody> <%foreach (mvcexamples.model.student student in model.getstudentlist) {%> <tr> <td> <%=student.sno %> </td> <td> <%=student.sname %> </td> <td> <%=student.ssex %> </td> <td> <%=student.sbirthday %> </td> <td> <%=student.sclass %> </td> <td> <a href="javascript:void(0);" class="a_update" sno="<%=student.sno %>" sname="<%=student.sname %>" ssex="<%=student.ssex %>" sbirthday="<%=student.sbirthday %>" sclass="<%=student.sclass %>">修改</a>   <a href="javascript:void(0);" class="a_delete" sno="<%=student.sno %>">删除</a> </td> </tr> <% } %> </tbody> <tfoot> <tr> <td> 全选 </td> <td colspan="5" style="text-align: right;"> <a href="javascript:void(0);" id="a_add">添加</a> </td> </tr> </tfoot> </table> <%=html.mikepager(model.getstudentlist)%> <br /> <table> <thead> <tr> <td> 学生表 </td> </tr> <tr> <td> 学号 </td> <td> 姓名 </td> <td> 性别 </td> <td> 生日 </td> <td> 班级 </td> </tr> </thead> <tbody> <%foreach (mvcexamples.model.student student in model.studentlist) {%> <tr> <td> <%=student.sno %> </td> <td> <%=student.sname %> </td> <td> <%=student.ssex %> </td> <td> <%=student.sbirthday %> </td> <td> <%=student.sclass %> </td> </tr> <% } %> </tbody> </table> <br /> <table> <thead> <tr> <td> 老师表 </td> </tr> <tr> <td> 编号 </td> <td> 姓名 </td> <td> 性别 </td> <td> 生日 </td> <td> 职称 </td> <td> 所在部门 </td> </tr> </thead> <tbody> <%foreach (mvcexamples.model.teacher teacher in model.teacherlist) {%> <tr> <td> <%=teacher.tno%> </td> <td> <%=teacher.tname%> </td> <td> <%=teacher.tsex%> </td> <td> <%=teacher.tbirthday%> </td> <td> <%=teacher.prof%> </td> <td> <%=teacher.depart%> </td> </tr> <% } %> </tbody> </table> <div id="window" style="display:none;"> <input type="hidden" id="sno" name="sno" value="" /> 姓名:<input type="text" id="sname" name="sname" /><br /> 性别:<input type="text" id="ssex" name="ssex" /><br /> 生日:<input type="text" id="sbirthday" name="sbirthday" onclick = "wdatepicker()" /><br /> 班级:<input type="text" id="sclass" name="sclass" /><br /> </div> </asp:content>
3、controller層程式碼
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.mvc.ajax; namespace mvcexamples.web.controllers { public class studentcontroller : controller { // // get: /student/ mvcexamples.bll.student _student = new mvcexamples.bll.student(); mvcexamples.bll.teacher _teacher = new mvcexamples.bll.teacher(); /// <summary> /// 演示 /// </summary> /// <param name="pi"></param> /// <param name="sclass"></param> /// <returns></returns> public actionresult index(int? pi, string sclass) { int pageindex = pi ?? 1; int pagesize = 5; string sclass = sclass == null ? "95031" : sclass; mvcexamples.web.models.studentmodels _studentmodels = new mvcexamples.web.models.studentmodels(); _studentmodels.studentlist = _student.getmodellist("sclass=" + sclass); _studentmodels.teacherlist = _teacher.getmodellist("tsex='男'"); _studentmodels.getstudentlist = new pagedlist<mvcexamples.model.student>(_student.getmodellist("sclass=" + sclass).asqueryable(), pageindex, pagesize); return view(_studentmodels);//返回一个model } /// <summary> /// 修改学生信息 /// </summary> /// <param name="no"></param> /// <param name="name"></param> /// <param name="sex"></param> /// <param name="birsthday"></param> /// <param name="sclass"></param> /// <returns></returns> public actionresult updatestudent(string no, string name, string sex, string birsthday, string sclass) { mvcexamples.model.student _student = new mvcexamples.model.student(); _student.sno = no; _student.sname = name; _student.ssex = sex; _student.sbirthday = convert.todatetime(birsthday); _student.sclass = sclass; _student.update(_student); jsonresult json = new jsonresult(); json.data = new { result = "true" }; return json; } /// <summary> /// 删除学生信息 /// </summary> /// <param name="no"></param> /// <returns></returns> public actionresult deletestudent(string no) { bool isdelete= _student.delete(no); jsonresult json = new jsonresult(); return json; if (isdelete) { json.data = new { result = "true" }; } else { json.data = new { result ="false" }; } return json; } /// <summary> /// 添加学生信息 /// </summary> /// <param name="no"></param> /// <param name="name"></param> /// <param name="sex"></param> /// <param name="birsthday"></param> /// <param name="sclass"></param> /// <returns></returns> public actionresult addstudent(string no, string name, string sex, string birsthday, string sclass) { mvcexamples.model.student _student = new mvcexamples.model.student(); _student.sno = no; _student.sname = name; _student.ssex = sex; _student.sbirthday = convert.todatetime(birsthday); _student.sclass = sclass; _student.add(_student); jsonresult json = new jsonresult(); json.data = new { result = "true" }; return json; } /// <summary> /// 提供弹出窗口的数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public actionresult windowdata(int id) { jsonresult json = new jsonresult(); //这里给json数据(这里只是演示,下面数据是模拟的) json.data = new { name = "张三", sex = "男" }; return json; } } }
4、兩個分頁輔助類pagedlist和mikepagerhtmlextensions
pagedlist輔助類
using system; using system.data; using system.configuration; using system.linq; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.xml.linq; using system.collections.generic; using system.collections.specialized; namespace system.web.mvc { public interface ipagedlist { int totalpage //总页数 { get; } int totalcount { get; set; } int pageindex { get; set; } int pagesize { get; set; } bool ispreviouspage { get; } bool isnextpage { get; } } public class pagedlist<t> : list<t>, ipagedlist { public pagedlist(iqueryable<t> source, int? index, int? pagesize) { if (index == null) { index = 1; } if (pagesize == null) { pagesize = 10; } this.totalcount = source.count(); this.pagesize = pagesize.value; this.pageindex = index.value; this.addrange(source.skip((index.value - 1) * pagesize.value).take(pagesize.value)); } public int totalpage { get { return (int)system.math.ceiling((double)totalcount / pagesize); } } public int totalcount { get; set; } /// <summary> /// /// </summary> public int pageindex { get; set; } public int pagesize { get; set; } public bool ispreviouspage { get { return (pageindex > 1); } } public bool isnextpage { get { return ((pageindex) * pagesize) < totalcount; } } } public static class pagination { public static pagedlist<t> topagedlist<t>(this iorderedqueryable<t> source, int? index, int? pagesize) { return new pagedlist<t>(source, index, pagesize); } public static pagedlist<t> topagedlist<t>(this iorderedqueryable<t> source, int? index) { return new pagedlist<t>(source, index, 10); } public static pagedlist<t> topagedlist<t>(this iqueryable<t> source, int? index, int? pagesize) { return new pagedlist<t>(source, index, pagesize); } public static pagedlist<t> topagedlist<t>(this iqueryable<t> source, int? index) { return new pagedlist<t>(source, index, 10); } } }
mikepagerhtmlextensions輔助類別
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Web.Mvc; using System.Web.Routing; using System.Text; namespace System.Web.Mvc { public static class MikePagerHtmlExtensions { #region MikePager 分页控件 public static string MikePager<T>(this HtmlHelper html, PagedList<T> data) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, object values) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action) { return MikePager<T>(html, data, action, null); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, object values) { string controllerName = html.ViewContext.RouteData.GetRequiredString("controller"); return MikePager<T>(html, data, action, controllerName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, object values) { return MikePager<T>(html, data, action, controller, new RouteValueDictionary(values)); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, RouteValueDictionary values) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, RouteValueDictionary values) { string controllerName = html.ViewContext.RouteData.GetRequiredString("controller"); return MikePager<T>(html, data, action, controllerName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, RouteValueDictionary valuedic) { int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1; int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage; RouteValueDictionary vs = valuedic == null ? new RouteValueDictionary() : valuedic; var builder = new StringBuilder(); builder.AppendFormat("<div class=\"mike_mvc_pager\">"); if (data.IsPreviousPage) { vs["pi"] = 1; builder.Append(Html.LinkExtensions.ActionLink(html, "首页", action, controller, vs, null)); builder.Append(" "); vs["pi"] = data.PageIndex - 1; builder.Append(Html.LinkExtensions.ActionLink(html, "上一页", action, controller, vs, null)); builder.Append(" "); } for (int i = start; i <= end; i++) //前后各显示5个数字页码 { vs["pi"] = i; if (i == data.PageIndex) { builder.Append("<font class='thispagethis'>" + i.ToString() + "</font> "); } else { builder.Append(" "); builder.Append(Html.LinkExtensions.ActionLink(html, i.ToString(), action, controller, vs, null)); } } if (data.IsNextPage) { builder.Append(" "); vs["pi"] = data.PageIndex + 1; builder.Append(Html.LinkExtensions.ActionLink(html, "下一页", action, controller, vs, null)); builder.Append(" "); vs["pi"] = data.TotalPage; builder.Append(Html.LinkExtensions.ActionLink(html, "末页", action, controller, vs, null)); } builder.Append(" 每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页 </div>"); return builder.ToString(); } #endregion } }
效果圖:
5、源碼下砸:jquery.ajax非同步實現增刪改查和分頁
以上就是本文的全部內容,希望對大家的學習有所幫助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP開發:如何實現表格資料排序和分頁功能在進行Web開發中,處理大量資料是一項常見的任務。對於需要展示大量資料的表格,通常需要實現資料排序和分頁功能,以提供良好的使用者體驗和最佳化系統效能。本文將介紹如何使用PHP實作表格資料的排序和分頁功能,並給出具體的程式碼範例。排序功能實作在表格中實作排序功能,可以讓使用者根據不同的欄位進行升序或降序排序。以下是一個實作表格

Go語言是一種高效、簡潔且易於學習的程式語言,因其在並發程式設計和網路程式設計方面的優勢而備受開發者青睞。在實際開發中,資料庫操作是不可或缺的一部分,本文將介紹如何使用Go語言實作資料庫的增刪改查操作。在Go語言中,我們通常會使用第三方函式庫來操作資料庫,例如常用的sql套件、gorm等。這裡以sql包為例介紹如何實作資料庫的增刪改查操作。假設我們使用的是MySQL資料庫。

如何使用JavaScript實作表格分頁功能?隨著網路的發展,越來越多的網站都會使用表格來展示數據。在某些資料量較大的情況下,需要將資料進行分頁展示,以提升使用者體驗。本文將介紹如何使用JavaScript實作表格分頁功能,並提供具體的程式碼範例。一、HTML結構首先,我們需要準備一個HTML結構來承載表格和分頁按鈕。我們可以使用<tab

快速應用:PHP非同步HTTP下載多個檔案的實用開發案例分析隨著互聯網的發展,檔案下載功能已成為許多網站和應用程式的基本需求之一。而對於需要同時下載多個檔案的場景,傳統的同步下載方式往往效率低且耗費時間。為此,使用PHP非同步HTTP下載多個檔案成為了越來越常見的解決方案。本文將透過一個實際的開發案例,詳細分析如何使用PHP非同步HTTP

MyBatis是一個優秀的持久層框架,它支援基於XML和註解的方式操作資料庫,簡單易用,同時也提供了豐富的插件機制。其中,分頁插件是使用頻率較高的插件之一。本文將深入探討MyBatis分頁外掛的原理,並結合具體的程式碼範例進行說明。一、分頁外掛原理MyBatis本身並沒有提供原生的分頁功能,但可以藉助外掛程式來實現分頁查詢。分頁插件的原理主要是透過攔截MyBatis

如何利用Layui開發一個具有分頁功能的資料展示頁面Layui是一個輕量級的前端UI框架,提供了簡潔美觀的介面元件和豐富的互動體驗。在開發中,我們經常會遇到需要展示大量資料並進行分頁的情況。以下是利用Layui開發的具有分頁功能的資料展示頁面的範例。首先,我們需要引入Layui的相關文件和依賴。在html頁面的<head>標籤中加入以下代

Vue元件實戰:分頁元件開發介紹在網路應用程式中,分頁功能是不可或缺的一個元件。一個好的分頁元件應該展示簡潔明了,功能豐富,而且易於整合和使用。在本文中,我們將介紹如何使用Vue.js框架來開發一個高度可自訂化的分頁元件。我們將透過程式碼範例來詳細說明如何使用Vue元件開發。技術堆疊Vue.js2.xJavaScript(ES6)HTML5和CSS3開發環

Vue是一種流行的JavaScript框架,用於建立使用者介面。在Vue技術開發中,實現分頁功能是常見的需求。本文將介紹如何使用Vue來實現分頁功能,並提供具體程式碼範例。在開始之前,我們需要提前準備一些基本知識。首先,我們需要了解Vue的基本概念和文法。其次,我們需要知道如何使用Vue元件來建立我們的應用程式。在開始之前,我們需要在Vue專案中安裝一個分頁插件,
