开发工具与关键技术:Visual Studio,MVC
作者:李建威
撰写时间:2019年07月28日
//获取要操作的行的数据
function getCheckedRecords() {
var records = new Array();
$("#tabProject tr").each(function () {
if ($(this).find("td:eq(0)>input[type='checkbox']:checked").length == 1) {
records[records.length] = tabProject.getRowRecord($(this));
}
});
return records;
}
//删除项目
$("#anNiuSCXM").click(function () {
var records = getCheckedRecords();
//声明变量用来储存被删除的数据条数
var ReturnLength = 0;
if (records.length > 0) {
layer.confirm("确定要删除选中的项目吗?", {
icon: 3,
btn: ['确定', '取消'],
offset: "100px"
}, function (layerIndex) {
//关闭提示窗口
layer.close(layerIndex);
for (var i = 0; i < records.length; i++) {
var ProjectID = tabProject.getRecordIndexValue(records[i], "ProjectID");
$.ajax({
url: "DeleteProject?ProjectID=" + ProjectID,
type: "get",//数据传输通道的类型
async: false,//同步加载:等待ajax请求返回结果再执行ajax后面的语句;true(默认)则相反
dataType: "json",//传输数据类型
success: function (data) {
if (data == "success") {
ReturnLength++;
if (ReturnLength == records.length) {
//刷新页面
doSearch();
layer.alert("删除成功", { icon: 1, title: "提示", offset: "100px" });
}
} else {
layer.alert(data, { icon: 5, title: "提示" });
}
}
});
}
});
} else {
layer.alert("请选择要删除的数据", { icon: 0, title: "提示", offset: "100px" });
}
});
控制器:
public ActionResult DeleteProject(int ProjectID)
{
string strMsg = “failed”;
try
{
var dbProjectBinding = myModels.S_ProjectBinding.Where(m => m.ProjectID == ProjectID).ToList();
var i = dbProjectBinding.Count();
for (var x = 0; x < i; x++)
{
myModels.S_ProjectBinding.Remove(dbProjectBinding[x]);
}
var dp = myModels.SaveChanges();
if (dp > 0||i==0)
{
var dbProject = myModels.S_Project.Where(m => m.ProjectID == ProjectID).Single();
myModels.S_Project.Remove(dbProject);
myModels.SaveChanges();
strMsg = "success";
}
}
catch (Exception)
{
strMsg = "不知道为什么,失败了";
}
return Json(strMsg, JsonRequestBehavior.AllowGet);
}