MVC jQueryAjax异步实现增删改查和分页.docx
《MVC jQueryAjax异步实现增删改查和分页.docx》由会员分享,可在线阅读,更多相关《MVC jQueryAjax异步实现增删改查和分页.docx(30页珍藏版)》请在冰点文库上搜索。
MVCjQueryAjax异步实现增删改查和分页
这篇文章主要为大家详细介绍了MVC结合jQuery.Ajax异步实现增删改查和分页的相关资料,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了MVC+jQuery.Ajax异步实现增删改查和分页的具体代码,供大家参考,具体内容如下
1、Model层代码
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Collections.Generic;
usingMvcExamples;
usingSystem.Web.Mvc;
namespaceMvcExamples.Web.Models
{
publicclassStudentModels
{
///<summary>
///获取学生信息列表
///</summary>
publicList<MvcExamples.Model.Student>StudentList{get;set;}
///<summary>
///获取教工信息列表
///</summary>
publicList<MvcExamples.Model.Teacher>TeacherList{get;set;}
///<summary>
///获取学生信息列表(分页)
///</summary>
publicPagedList<MvcExamples.Model.Student>GetStudentList{get;set;}
}
}
2、View层代码
<%@PageTitle=""Language="C#"MasterPageFile="~/Views/Shared/Site.Master"Inherits="System.Web.Mvc.ViewPage<MvcExamples.Web.Models.StudentModels>"%>
<asp:
ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server">
Index
</asp:
Content>
<asp:
ContentID="Content3"ContentPlaceHolderID="HeadContent"runat="server">
<scriptsrc="type="text/javascript"></script>
<scriptsrc="type="text/javascript"></script>
<scriptsrc="type="text/javascript"></script>
<linkhref="rel="stylesheet"type="text/css"/>
<scripttype="text/javascript">
$(function(){
//添加学生信息
$('#a_add').click(function(){
$('#window').dialog({
title:
"添加学生信息",
width:
300,
height:
200,
modal:
true,
buttons:
{
"取消":
function(){
$(this).dialog("close");//当点击取消按钮时,关闭窗口
},
"添加":
function(){
//当点击添加按钮时,获取各参数的值
varsno=$('#sno').val();
varsname=$('#sname').val();
varssex=$('#ssex').val();
varsbirsthday=$('#sbirthday').val();
varsclass=$('#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(){
varstudent=$(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(){
varsno=$('#sno').val();
varsname=$('#sname').val();
varssex=$('#ssex').val();
varsbirsthday=$('#sbirthday').val();
varsclass=$('#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:
ContentID="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.StudentstudentinModel.GetStudentList)
{%>
<tr>
<td>
<%=student.sno%>
</td>
<td>
<%=student.sname%>
</td>
<td>
<%=student.ssex%>
</td>
<td>
<%=student.sbirthday%>
</td>
<td>
<%=student.sclass%>
</td>
<td>
<ahref="javascript:
void(0);"class="a_update"sno="<%=student.sno%>"sname="<%=student.sname%>"ssex="<%=student.ssex%>"
sbirthday="<%=student.sbirthday%>"sclass="<%=student.sclass%>">修改</a>
<ahref="javascript:
void(0);"class="a_delete"sno="<%=student.sno%>">删除</a>
</td>
</tr>
<%}%>
</tbody>
<tfoot>
<tr>
<td>
全选
</td>
<tdcolspan="5"style="text-align:
right;">
<ahref="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>
生