This example for you to share the Mvc+jquery.ajax asynchronous implementation of additions and deletions to check and pagination of the specific code for your reference, the specific contents are as follows
1, model layer code
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>
/// Get student Information list
///</summary> public
list<mvcexamples.model.student> studentlist {getting; set;}
<summary>
///Get Faculty Information list
///</summary> public
list<mvcexamples.model.teacher> teacherlist {get; set;}
<summary>
///Get student Information list (paging)
///</summary> public
pagedlist< Mvcexamples.model.student> getstudentlist {get; set;}}}
2, view layer code
<%@ 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 () {//Add student Information $ (' #a_add '). Click (function () {$ (' #window '). Dialog ({title: "Add student Information", Width:3height:200, Modal:true, buttons: {"Cancel": function () {$ (this). dialog ("Close");//When Clicked Cancel Press
button, close the window}, add: Function () {//when clicking the Add button, get the value of each parameter 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 ',//path to add method data: ' no= ' +sno+ ' &name= ' +sname+ ' &sex = ' +ssex+ ' &birsthday= ' +sbirsthday+ ' &sclass= ' +sclass,//the number of parameters and name to be consistent with the name of the method Success:function (JSON)//
Returns the {$ (' #window ') of the JSON type. dialog ("Close");
Determines whether success if (json.result== "true") {$ (' #btn_close '). Click (); Alert (' Congratulations, change success!
'); }else{alert (' Sorry, change failed!
');
}
}
});
}
}
}); ///Delete student information $ ('. A_delete '). Click (function () {//Confirm whether to delete the IF (confirm) Delete this message?{$.ajax ({type: ' post '), url: '/student/deletestudent ', data: ' no= ' +$ (This). attr ("Sno"),//Get the properties of the current object (custom attribute) Sno The value of the corresponding data sucess:function (JSON) {if (json.result== "true") {alert ("Congratulations! , has been successfully deleted!
"); }else {alert ("Sorry, delete failed!")
");
}}})//modify student Information $ ('. 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: "Modify student Information", width:300, height:200, Modal:true, buttons: {"Cancel": F
Unction () {$ (this). dialog (' Close ');
"Modify": 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 ("Clos
E ");
if (json.result== "true") {$ (' #btn_close '). Click (); Alert (' Congratulations, change success!
'); }else{alert (' Sorry, change failed!
');
}
}
});
}
}
});
}); }) </script> </asp:Content> <asp:content id= "Content2" contentplaceholderid= maincontent "runat=" Server >
3, controller layer code
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/mvcexa Mples. Bll.
Student _student = new MvcExamples.BLL.Student ();
MvcExamples.BLL.Teacher _teacher = new MvcExamples.BLL.Teacher (); <summary>///demo///</summary>///<param name= "PI" ></param>///<param name= "Scla SS "></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= ' male"); _studentmodels.getstudentList = new Pagedlist<mvcexamples.model.student> (_student.getmodellist ("sclass=" + sclass).
AsQueryable (), PageIndex, PageSize); Return View (_studentmodels);//Returns a model}///<summary>///modify student information///</summary>///<param Name = "No" ></param>///<param name= "name" ></param>///<param name= "Sex" ></param>///& Lt;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>///Delete Student information///</summary>///<param name= "No" ></param>///<returns></re
Turns> 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>///Add student information///</summary>///<param name= "No" ></param>///<param na Me= "Name" ></param>///<param name= "Sex" ></param>///<param name= "Birsthday" ></param&
Gt <param name= "Sclass" ></param>///<returns></returns> public actionresult addstudent (stri Ng No, string name, String sex, String birsthday, String sclass) {MvcExamples.Model.Student _student = new Mvcexampl Es.
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>///provides pop-up window data///</summary>///<param name= "id" ></param>///<retu
Rns></returns> public actionresult windowdata (int id) {Jsonresult json = new Jsonresult (); Here's the JSON data (here just demo, the data below is simulated) JSON.
Data = new {name = "John", sex = "male"};
return JSON;
}
}
}
4, two paging auxiliary class pagedlist and Mikepagerhtmlextensions
Pagedlist Auxiliary class
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//total pages {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 Iorderedqueryab le<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) {Retu
RN New pagedlist<t> (source, index, 10); } public static pagedlist<t> ToPageDlist<t> (this iqueryable<t> source, int. index, int? pageSize) {Return to New pagedlist<t> (source, in
Dex, PageSize); public static pagedlist<t> topagedlist<t> (this iqueryable<t> source, int. Index) {return new
Pagedlist<t> (source, index, 10);
}
}
}
mikepagerhtmlextensions Auxiliary class
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 paging control public static St Ring 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 A Ctioinname = HTML.
ViewContext.RouteData.GetRequiredString ("action");
return mikepager<t> (HTML, data, Actioinname, values); The 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 controll
Er, 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); The 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 controll Er, 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, "Home", action, Controller, VS, NULL)); Builder.
Append (" "); vs["PI" = data.
PageIndex-1; Builder. Append (Html.LinkExtensions.ActionLink (Html, prev), Action, controlLer, VS, NULL)); Builder.
Append (" ");
for (int i = start; I <= end; i++)//before and after displaying 5 number page numbers {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, next page, action, controller, VS, NULL)); Builder.
Append (" "); vs["PI" = data.
Totalpage; Builder.
Append (Html.LinkExtensions.ActionLink (Html, "Last", action, Controller, VS, NULL)); } builder. Append (" per page" + data.) PageSize + "Strip/Total" + data. TotalCount + "ARTICLE" + data. PageIndex + "page/Total" + data.
Totalpage + "page </div>"); Return builder.
ToString ();
} #endregion}}
Effect Chart:
5, the source code to hit:jquery.ajax asynchronous Implementation, delete, check and pagination
The above is the entire content of this article, I hope to help you learn.