mvc视图增删改查
----------------显示分页
<h2>Show</h2>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-3.3.1.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>爱好</td>
</tr>
<tbody id="tbod"></tbody>
</table>
</div>
</body>
</html>
<a href="#" onclick="Show('first')">首页</a>
<a href="#" onclick="Show('prev')">上一页</a>
<a href="#" onclick="Show('next')">下一页</a>
<a href="#" onclick="Show('last')">尾页</a>
<input type="hidden" id="pageIndex" />
<input type="hidden" id="totalPage" />
<script>
Show('first');
function Show(page) {
switch (page) {
case 'first':
$('#pageIndex').val(1);
break;
case 'prev':
var num = Number($("#pageIndex").val());
if (num > 1) {
$("#pageIndex").val(num - 1);
}
else {
$("#pageIndex").val(1);
}
break;
case 'next':
var num = Number($("#pageIndex").val());
if (num < Number($("#totalPage").val())) {
$("#pageIndex").val(num + 1);
}
else {
$("#pageIndex").val($("#totalPage").val());
}
break;
case 'last':
$("#pageIndex").val($("#totalPage").val());
break;
default: break;
}
var b = $("#pageIndex").val();
$.ajax({
url: "http://localhost:63073/api/News/GetEmperors?pageIndex"+2,
type: "get",
dataType: "json",
success: function (data) {
$("#totalPage").val(data.count);
$("table tr:gt(0)").remove();
$(data).each(function () {
var str = '<tr>' +
'<td>' + this.NId + '</td>' +
'<td>' + this.NName + '</td>' +
'<td>' + this.NSourse + '</td>' +
'</tr>';
$("#tbod").append(str);
})
}
})
}
</script>
-----------------------------删除
function Del(Id) {
if (confirm("确定要删除吗")) {
$.ajax({
success: function (d) {
if (d > 0) {
alert("删除成功");
location.href = '/Default/Show';
}
else {
alert("失败");
}
}
})
}
}
---------------------------------------添加
function Add() {
var obj = {
Name: $("#Text1").val(),
Zhon: $("#Text2").val(),
identit: $("#Text3").val(),
DDate: $("#Text4").val(),
DMoney: $("#Text5").val()
}
$.ajax({
url: "--------------",
type: "post",
dataType: "json",
data: obj,
success: function (d) {
if (d > 0) {
alert("添加成功");
location.href = '/Default/Show';
}
}
})
}
--------------------
$(function () {
FanTian();
})
--------------------------------反填
function () {
$.ajax({
async: false,
url: "-----------------",
type: "get",
dataType: "json",
data:{id:$("#hid_Id").val()},
success: function (d) {
console.log(d);
$("#txt_Name").val(d.Name);
$("#hid_Id").val(d.Id);
$("#ta_Content").val(d.StudyContent);
$("#hid_Time").val(d.StudyTime);
$("#sel_Type option").each(function () {
if (this.value == d.TId) {
$(this).prop("selected", "selected");
}
})
}
})
}
-----------------------------------保存修改
function SaveStudy() {
var obj = {
Name: $("#txt_Name").val(),
TId: $("#sel_Type").val(),
StudyContent: $("#ta_Content").val(),
StudyTime: $("#hid_Time").val(),
Id: $("#hid_Id").val()
}
if (obj.Name == "") {
alert("姓名不能为空");
return;
}
if (obj.TId== "") {
alert("账号不能为空");
return;
}
if (obj.StudyContent== "") {
alert("身份证号不能为空");
return;
}
if (obj.StudyTime== "") {
alert("联系电话不能为空");
return;
}
$.ajax({
async: false,
url: "---------",
dataType: "json",
type: "post",
data: obj,
success: function (d) {
if (d > 0) {
alert("修改成功");
location.href = '/Study/Index';
}
else{
alert("修改失败");
}
}
})
}
</script>
<a href="#" onclick="Show('first')">首页</a>
<a href="#" onclick="Show('prev')">上一页</a>
<a href="#" onclick="Show('next')">下一页</a>
<a href="#" onclick="Show('last')">尾页</a>
<input type="hidden" id="pageIndex" />
<input type="hidden" id="totalPage" />
<script>
Show('first');
function Show(page) {
switch (page) {
case 'first':
$('#pageIndex').val(1);
break;
case 'prev':
var num = Number($("#pageIndex").val());
if (num > 1) {
$("#pageIndex").val(num - 1);
}
else {
$("#pageIndex").val(1);
}
break;
case 'next':
var num = Number($("#pageIndex").val());
if (num < Number($("#totalPage").val())) {
$("#pageIndex").val(num + 1);
}
else {
$("#pageIndex").val($("#totalPage").val());
}
break;
case 'last':
$("#pageIndex").val($("#totalPage").val());
break;
default: break;
}
var b = $("#pageIndex").val();
$.ajax({
url: "http://localhost:57480/api/default/GetEmperors?pageIndex=" + 2,
type: "get",
dataType: "json",
success: function (data) {
$("#totalPage").val(data.count);
$("table tr:gt(0)").remove();
$(data).each(function () {
var str = '<tr>'
+ '<td>' + this.Id + '</td>'
+ '<td>' + this.Name + '</td>'
+ '<td>' + this.Zhon + '</td>'
+ '<td><input id="Button1" type="button" value="删除" onclick="Del(' + this.Id + ')"/></td > '
+ '</tr>';
$("#tbod").append(str);
})
}
})
}