public int gradeadd(grade model)
{
string sql = string.Format("insert into greade(studentname,sex,classgrade,math,chinese,English) values('{0}','{1}','{2}','{3}','{4}','{5}')", model.studentname,model.sex,model.classgrade,model.math, model.chinese, model.English);
return db.ExecuteNonQuery(sql);
}
public DataTable gradeselect() {
string str = "select * from greade";
DataTable dt = db.GetDataTable(str);
return dt;
}
public int del(int id)
{
string str = "delete from greade where id=" + id;
return db.ExecuteNonQuery(str);
}
public grade gradeft(int id)
{
string str = "select * from greade where id=" + id;
DataTable dt = db.GetDataTable(str);
grade g = dt.AsEnumerable().Select(p => new grade()
{
id = p.Field<int>("id"),
studentname = p.Field<string>("studentname"),
sex = p.Field<string>("sex"),
classgrade = p.Field<string>("classgrade"),
math = p.Field<string>("math"),
chinese = p.Field<string>("chinese"),
English = p.Field<string>("English")
}).FirstOrDefault();
return g;
}
public int greadupdate(grade model)
{
string str = string.Format("update greade set studentname='{0}',sex='{1}',classgrade='{2}',math='{3}',chinese='{4}',English='{5}' where id='{6}'", model.studentname, model.sex, model.classgrade,model.math,model.chinese,model.English,model.id);
return db.ExecuteNonQuery(str);
}
控制器面板
public ActionResult addd()
{
return View();
}
[HttpPost]
public int gradeadd(grade model)
{
return bll.gradeadd(model);
}
public ActionResult gradeselect()
{
return View();
}
const int pageindex = 3;
[HttpGet]
public JsonResult gradeselect2(int page=1)
{
DataTable nn = bll.gradeselect();
var jt = JsonConvert.SerializeObject(nn);
var jj = JsonConvert.DeserializeObject<List<grade>>(jt);
page p = new page();
p.pages = page;
p.pagecount = (jj.Count() / pageindex) + (jj.Count() % pageindex == 0 ? 0 : 1);
p.data = jj.Skip((page - 1) * pageindex).Take(pageindex).ToList();
return Json(p, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public int del(int id)
{
return bll.del(id);
}
public ActionResult greadeupdate()
{
return View();
}
[HttpGet]
public JsonResult gradeft(int id)
{
return Json( bll.gradeft(id),JsonRequestBehavior.AllowGet);
}
[HttpPost]
public int greadupdate(grade model)
{
return bll.greadupdate(model);
}
table下面
<a href="javascript:;" onclick="first()">首页</a>
<a href="javascript:;" onclick="prev()">上一页</a>
<a href="javascript:;" onclick="next()">下一页</a>
<a href="javascript:;" onclick="last()">尾页</a>
<script>
var pageindex = 1;
var pagecount = 0;
$(function () {
Datainfo();
})
function Datainfo () {
$.ajax({
url: "/user/gradeselect2?page=" + pageindex,
type: "get",
success: function (data) {
pageindex = data.pages;
pagecount = data.pagecount;
$("#tab tr").not(":first").remove();
$(data.data).each(function (index, data) {
var tr = '<tr>'
+ '<td>' + data.studentname+'</td>'
+ '<td>' + data.sex+'</td>'
+ '<td>' + data.classgrade+'</td>'
+ '<td>' + data.math+'</td>'
+ '<td>' + data.chinese+'</td>'
+ '<td>' + data.English + '</td>'
+ '<td><a href="#" onclick="del(' + data.id +')">删除</a><input id="Button1" type="button" value="修改" onclick="upt('+data.id+')" /></td>'
+ '</tr>'
$("#tab").append(tr);
})
}
})
}
function del(id) {
if (confirm("是否删除")) {
$.ajax({
url: "/user/del?id="+id,
type: "post",
success: function (data) {
if (data > 0) {
alert("删除成功")
}
}
})
}
}
function upt(id) {
location.href = "/user/greadeupdate?ID=" + id;
}
function first() {
pageindex = 1;
Datainfo();
}
function prev() {
if (pageindex == 1) {
alert("已经是首页了");
}
else {
pageindex--;
Datainfo();
}
}
function next() {
if (pageindex == pagecount) {
alert("已经到尾页");
}
else {
pageindex++;
Datainfo();
}
}
function last() {
pageindex = pagecount;
Datainfo();
}
</script>
<input id="Hidden1" type="hidden" value="@Request.QueryString["id"]" name="id"/>
<script>
$(function () {
$.ajax({
url: "/user/gradeft?id="+@Request.QueryString["id"],
type: "get",
success: function (data) {
$("#studentname").val(data.studentname),
$("#sex").val(data.sex),
$("#classgrade").val(data.classgrade),
$("#math").val(data.math),
$("#chinese").val(data.chinese),
$("#English").val(data.English)
}
})
})
function fun() {
$.ajax({
url: "/user/greadupdate",
type: "post",
data: $("#tt").serialize(),
success: function (data) {
alert(data);
if (data > 0) {
alert("修改成功")
}
}
})
}
</script>