前台
<input id="Button1" type="button" value="button" onclick="add()" />
<input id="save" onclick="saveadd()" type="button" value="批量保存" />
<div>
<table>
<tr>
<td><input type="checkbox" checked='checked'/></td>
<td>编号</td>
<td>学号</td>
<td>名称</td>
<td>年龄</td>
</tr>
<tbody id="tname">
</tbody>
</table>
</div>
<script>
$(function () {
show();
})
function show() {
debugger;
$.ajax({
url: "/show/add",
type: "get",
dataType: "json",
success: function ( data) {
console.log(data);
alert(data);
debugger;
var str = '';
for (var i = 0; i < data.length; i++) {
str += " <tr>";
str += "<td><input type="checkbox" checked='checked'/></td>";
str += "<td>" + data[i].id + "</td>";
str += "<td>" + data[i].xuehao + "</td>";
str += "<td>" + data[i].name + "</td>";
str += "<td>" + data[i].age + "</td>";
str += "<td> <input id='del' type="button" class="btn btn-primary" onclick="del(" + data[i].id + ")" value="删除" /></td>";
str += "</tr>";
}
$("#tname").append(str);
}
})
}
//添加
function add()
{
//var ss = Date.now();
var ss = Date.now();
debugger
var str = "";
str += " <tr class='ss'>";
str += "<td><input type="checkbox" checked='checked'/></td>";
str += "<td><input id="Text2" type="text" /></td>";
str += "<td><input id="Text3" type="text" /></td>";
str += "<td><input id="Text4" type="text" /></td>";
str += "<td><input id='del' type="button" class="btn btn-primary" value="删除" /></td>";
str += "</tr>";
$("#tname").append(str);
}
function saveadd()
{
var arry = [];
$(".ss").each(function () {
if ($(this).children().eq(0).children().first().prop("checked") == true)
{
var ss = $(this).children().eq(1).children().first().val();
var sss = $(this).children().eq(2).children().first().val();
var ssss = $(this).children().eq(3).children().first().val();
arry.push(JSON.stringify({ xuehao: ss, name: sss, age: ssss }));
}
})
$.ajax({
url: "/show/ad",
type: "post",
traditional: true,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
data: { arry: arry },
success: function (data) {
if (data > 0) {
alert("添加成s功!");
show();
}
}
})
}
</script>
后台
//显示
public ActionResult add()
{
var data = new BLL.stuBLL().show();
var data2 = JsonConvert.SerializeObject(data);
return Content(data2);
}
//添加
[HttpPost]
public ActionResult ad(string[] arry)
{
var str = "insert into student values";//添加数据库拼接
stuModel mm = new stuModel();//类
var data = arry.ToList();
var ass = "";
for (int i = 0; i < data.Count; i++)//循环
{
ass = data[i];
var person1 = JsonConvert.DeserializeAnonymousType(ass, mm);//把json转换为mm类
str += "( '" + person1.xuehao + "','" + person1.name + "', '" + person1.age + "') ,";
}
int chu = str.LastIndexOf(',');
var sss = str.Substring(0, chu - 1);
int cg = new BLL.stuBLL().add(sss);
return Content(cg.ToString());
}