///页面
<div>
@using (Html.BeginForm())
{
<input id="txtname" type="text" /><input id="Button1" type="button" value="查询" onclick="Select()" class="btn btn-primary"/>
<input type="button" id="AddBtn" value="添加 " class="btn btn-primary" />
<input type="button" id="BaoCunBtn" value="保存 " class="btn btn-primary"/>
<table id="table1" class="table table-bordered">
<thead>
<tr>
<td>编号</td>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tbody1"></tbody>
</table>
}
</div>
///ajax请求
<script>
//显示方法
function ShowList() {
$.ajax({
url: "/Student/StudentList",
type: "post",
success: function (data) {
$("#tbody1").html(" ");
var str = "";
for (var i = 0; i < data.length; i++) {
str += "<tr>"
str += "<td>" + data[i].ID + "</td>"
str += "<td>" + data[i].XueHao + "</td>"
str += "<td>" + data[i].StuName + "</td>"
str += "<td>" + data[i].Age + "</td>"
str += "<td><input type=\"button\" value=\"删除存储过程\" onclick=\"DeleteCCGC(" + data[i].ID + ")\" class=\"btn btn-primary\"/>"
str += "<input type=\"button\" value=\"删除\" onclick=\"Delete(" + data[i].ID + ")\" class=\"btn btn-primary\"/>"
str += "<input type=\"button\" class=\"Update\" value=\"修改\" class=\"btn btn-primary\" />"
str += "<input type=\"button\" class=\"BaoCun\" value=\"保存\" style=\"display:none;\" class=\"btn btn-primary\" />";
str += "<input type=\"button\" class=\"QuXiao\" value=\"取消\" style=\"display:none;\" class=\"btn btn-primary\" /></td>";
str += "</tr>";
}
$("#tbody1").append(str);
}
})
}
//删除
function Delete(id) {
$.ajax({
url: "/Student/Delete?id=" + id,
type: "post",
success: function (data) {
if (data.code == 1) {
alert(data.msg);
ShowList();
}
else {
alert(data.msg);
}
}
})
}
//删除存储过程
function DeleteCCGC(id) {
$.ajax({
url: "/Student/DeleteCCGC?id="+id,
type: "post",
success: function (data) {
if (data.code == 1) {
alert("删除成功!");
ShowList();
}
else {
alert("删除成功!");
}
}
})
}
//查询方法
function Select() {
var name=$("#txtname").val();
$.ajax({
url: "/Student/Select?name="+name,
type: "post",
success: function (data) {
$("#tbody1").html(" ");
var str = "";
for (var i = 0; i < data.length; i++) {
str += "<tr>"
str += "<td>" + data[i].ID + "</td>"
str += "<td>" + data[i].XueHao + "</td>"
str += "<td>" + data[i].StuName + "</td>"
str += "<td>" + data[i].Age + "</td>"
str += "<td><input type=\"button\" value=\"删除存储过程\" onclick=\"DeleteCCGC(" + data[i].ID + ")\" class=\"btn btn-primary\"/>"
str += "<input type=\"button\" value=\"删除\" onclick=\"Delete(" + data[i].ID + ")\" class=\"btn btn-primary\"/>"
str += "<input type=\"button\" class=\"Update\" value=\"修改\" class=\"btn btn-primary\" />"
str += "<input type=\"button\" class=\"BaoCun\" value=\"保存\" style=\"display:none;\" class=\"btn btn-primary\" />";
str += "<input type=\"button\" class=\"QuXiao\" value=\"取消\" style=\"display:none;\" class=\"btn btn-primary\" /></td>";
str += "</tr>";
}
$("#tbody1").append(str);
}
})
}
$(function () {
//显示
ShowList();
//追加行方法1
$("#AddBtn").click(function () {
var HtmlStr = "";
HtmlStr += "<tr class=\"AddTr\">"
HtmlStr += "<td></td>"
HtmlStr += "<td><input class=\"\" type=\"text\" /></td>"
HtmlStr += "<td><input class=\"txtName\" type=\"text\" /></td>"
HtmlStr += "<td><input class=\"txtAge\" type=\"text\" /></td>"
HtmlStr += "<td></td>"
HtmlStr += "</tr>"
$("#tbody1").append(HtmlStr)
})
//追加行方法2
//$("#AddBtn").click(function () {
// $("#tbody1").append(
// "<tr class=\‘AddTr\‘>"
// + "<td><input class=\"txtID\" type=\"text\" /></td>"
// + "<td><input class=\"txtXueHao\" type=\"text\" /></td>"
// + "<td><input class=\"txtName\" type=\"text\" /></td>"
// + "<td><input class=\"txtAge\" type=\"text\" /></td>"
// + "<td><input id=\"DeleteBtn\" type=\"button\" value=\"删除\" class=\"btn btn-primary\"/></td>"
// + "</tr>"
// )
//})
//保存
$("#BaoCunBtn").click(function () {
var str=""
$(".AddTr").each(function(){
var xuehao=$(this).find("input").eq(0).val();
var name=$(this).find("input").eq(1).val();
var age=$(this).find("input").eq(2).val();
str += xuehao + ","+ name + ","+ age + "|";
})
//alert(str);
$.ajax({
url: "/Student/Add?str=" + str,
type: "post",
dataType: "json",
success: function (data) {
if (data.code == 1) {
alert(data.msg);
ShowList();
}
else {
alert(data.msg);
}
}
})
})
//修改
$(document).on("click", ".Update", function () {
//显示保存和取消按钮
$(this).hide(); //修改按钮隐藏
$(this).siblings().eq(0).hide();//删除按钮隐藏
$(this).siblings().eq(1).hide(); //删除按钮隐藏
$(this).siblings().eq(2).show(); //保存按钮显示
$(this).siblings().eq(3).show(); //取消按钮显示
var id = $(this).parent().siblings().eq(0).html();//获取学号
var xuehao = $(this).parent().siblings().eq(1).html();//获取学号
var name = $(this).parent().siblings().eq(2).html();//获取姓名
var age = $(this).parent().siblings().eq(3).html();//获取年龄
//修改当前行ta为文本框并显示值
$(this).parent().siblings().eq(1).html("<input type=\"text\" class=\"txtXueHao\" value=\"" + xuehao + "\" />")
$(this).parent().siblings().eq(2).html("<input type=\"text\" class=\"txtName\" value=\"" + name + "\" />")
$(this).parent().siblings().eq(3).html("<input type=\"text\" class=\"txtAge\" value=\"" + age + "\" />")
});
//修改保存
$(document).on("click", ".BaoCun", function () {
var id = $(this).parent().siblings().eq(0).html();
var xuehao = $(this).parent().siblings().find("input[class=‘txtXueHao‘]").val();
var name = $(this).parent().siblings().find("input[class=‘txtName‘]").val();
var age = $(this).parent().siblings().find("input[class=‘txtAge‘]").val();
//alert(id+","+xuehao + "," + name + "," + age);
$.ajax({
url: "/Student/Update",
type: "post",
dataType: "json",
data: { id: id, xuehao: xuehao, name: name, age: age },
success: function (data) {
if (data.code == 1) {
alert(data.msg);
ShowList();
}
else {
alert(data.msg);
}
}
})
})
//修改保存
$(document).on("click", ".QuXiao", function () {
alert("取消修改");
ShowList();
})
})
</script>
///控制器方法
/// <summary>
/// 显示页面
/// </summary>
/// <returns></returns>
public ActionResult StudentListShow()
{
return View();
}
/// <summary>
/// 显示方法
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult StudentList()
{
var result = new BLL.StudentBLL().Show("");
return Json(result);
}
/// <summary>
/// 添加方法
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[HttpPost]
public JsonResult Add(string str)
{
str=str.TrimEnd(‘|‘);
string[] array = str.Split(‘|‘);
if (array == null || array.Length == 0)
{
return Json(new { code = 0, msg = "字符串异常" });
}
List<StudentModel> list = new List<StudentModel>();
foreach (var item in array)
{
string[] arr = item.Split(‘,‘);
if (arr == null || arr.Length == 0)
{
return Json(new { code = 0, msg = "字符串异常" });
}
else
{
StudentModel s = new StudentModel();
s.XueHao = arr[0];
s.StuName = arr[1];
s.Age = Convert.ToInt32(arr[2]);
list.Add(s);
}
}
var result = new BLL.StudentBLL().AddList(list);
if (result > 0)
{
return Json(new { code = 1, msg = "添加成功" });
}
else
{
return Json(new { code = 0, msg = "添加失败" });
}
}
/// <summary>
/// 删除存储过程
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public JsonResult DeleteCCGC(int id)
{
var result = new BLL.StudentBLL().DeleteCCGC(id);
if (result > 0)
{
return Json(new { code = 1, msg = "删除成功" });
}
else
{
return Json(new { code = 0, msg = "删除失败" });
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public JsonResult Delete(int id)
{
var result = new BLL.StudentBLL().Delete(id);
if (result > 0)
{
return Json(new { code = 1, msg = "删除成功" });
}
else
{
return Json(new { code = 0, msg = "删除失败" });
}
}
/// <summary>
/// 修改方法
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[HttpPost]
public JsonResult Update(int id,string xuehao,string name,int age)
{
StudentModel stu = new StudentModel();
stu.ID = id;
stu.XueHao = xuehao;
stu.StuName = name;
stu.Age = age;
var result = new BLL.StudentBLL().Update(stu);
if (result > 0)
{
return Json(new { code = 1, msg = "修改成功" });
}
else
{
return Json(new { code = 0, msg = "修改失败" });
}
}
/// <summary>
/// 查询方法
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult Select(string name)
{
var result = new BLL.StudentBLL().Show(name);
return Json(result);
}
///添加事物DAL写法
/// <summary>
/// 添加(事物)
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int AddList(List<StudentModel> m)
{
conn.Open();
using (SqlTransaction st = conn.BeginTransaction())
{
try
{
int i = 0;
foreach (var item in m)
{
string sql = "INSERT INTO StudentInfo VALUES(‘" + item.XueHao + "‘,‘" + item.StuName + "‘," + item.Age + ")";
SqlCommand com = new SqlCommand(sql, conn);
com.Transaction = st;
i = com.ExecuteNonQuery();
}
st.Commit();
conn.Close();
return i;
}
catch
{
st.Rollback();
conn.Close();
return 0;
}
}
}
////删除存储过程DAL写法
/// <summary>
/// 删除存储过程
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int DeleteCCGC(int id)
{
SqlCommand com = new SqlCommand("Pro_Delete", conn);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ID", id);
com.Parameters.AddWithValue("@return_value", SqlDbType.Int);
com.Parameters["@return_value"].Direction = ParameterDirection.ReturnValue;
conn.Open();
com.ExecuteNonQuery();
var i =(int) com.Parameters["@return_value"].Value;
return i;
}
///创建删除存储过程
CREATE PROCEDURE Pro_Delete
@ID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DELETE FROM StudentInfo WHERE [email protected];
RETURN @@ROWCOUNT
END
GO
<td><input type="checkbox" id="CheckAll" />全选</td>
<td><input type="checkbox" name="CheckChild" value="@item.Gid " /></td>
//全选
$("#CheckAll").click(function(){
$("input[name=‘CheckChild‘]").prop("checked",this.checked);
})