bootstrap adminlte教程8:问答系统的ClubContents的删除方法
1,在index页面,分析要是Super角色用户,给一个Del按钮,点一下进行删除
@if (Session["role"].ToString() == "Super") { <form action="/ClubContents/DelClub?Id=@item.Id" method="post" class="col-md-1 col-xs-3 text-left"> <input id="DelSubmit" class="btn btn-xs btn-warning" type="submit" value="Del"> </form> }
上面用了一个JS确认
@section Scripts { <script type="text/javascript"> window.onload = function () { var bt = document.getElementById("DelSubmit"); bt.onclick = function () { confirm("真的要删除吗?"); } } </script> }
在后台增加方法
// POST: 删除问答 [HttpPost] //[ValidateAntiForgeryToken] public ActionResult DelClub(string id) { ///注意要删除二次,一次是相关回复,一次是提问 ClubContent clubContent = db.ClubContents.Find(id); db.ClubContents.Remove(clubContent); //删除回复 var clubContent1 = db.ClubContents.Where(c => c.ReplyID == id); db.ClubContents.RemoveRange(clubContent1); db.SaveChanges(); return RedirectToAction("Index"); }