这里还是用的是学院管理系统的案例
这是主页的删除请求代码
function deleteSpecialty(specialtyID)
{
layer.confirm("您确定要删除该专业信息吗?",
function (index)
{
//确定按钮的回调
ajax("@Url.Content("~/BaseInfoManage/Specialty/DeleteSpecialty")",
{
"specialtyID": specialtyID
}, function (jsonMsg) {
layer.alert(jsonMsg.Text);
if (jsonMsg.State) {
//刷新表格
tabSpecialtySearch();
}
},'post');
},
function (index) {
//取消按钮的回调
});
}
这是MVC控制台的代码
public ActionResult DeleteSpecialty(int specialtyID)
{
ReturnJson msg = new ReturnJson();
//判断该专业信息是否在使用
int classCount = myModel.SYS_Class.Count(o => o.specialtyID == specialtyID);
int studentCount = myModel.SYS_Student.Count(o => o.specialtyID == specialtyID);
//如果相加为0则执行
if (classCount + studentCount == 0)
{
try
{
SYS_Specialty dbSpecialty = myModel.SYS_Specialty.Single(o => o.specialtyID == specialtyID);
//从数据库中删除
myModel.SYS_Specialty.Remove(dbSpecialty);
if (myModel.SaveChanges()>0)
{
msg.State = true;
msg.Text = "删除成功";
}
else
{
msg.Text = "删除失败";
}
}
catch (Exception e)
{
Debug.WriteLine(e);
msg.Text = "删除异常";
}
}
else
{
msg.Text = "该专业信息在使用中,无法删除";
}
return Json(msg, JsonRequestBehavior.AllowGet);
}