删除实例 view层调用Controller中Action删除 MVC AJAX JSON

View层

View Code
1 <a onclick="Del('@item.ID')" href="javascript:void(0)">删除</a>
2
3  <script type="text/javascript" language="javascript">
4 function Del(id) {
5 var warning = "您确定要删除吗?";
6 var flag = true;
7 //调用controller中的方法,判断是否允许删除
8   if (confirm(warning)) {
9 $.ajax({
10 type: 'post',
11 async: false, //是否异步
12   url: '/Controller/AllowDelete', // '/Controller/Action'
13 data: {id:id},//{参数:参数值}
14 success:function(data){
15 if(data=="true")
16 {
17 flag=true;
18 }
19 else
20 {
21 flag=false;
22 }
23 }
24 })
25 if(flag==false){
26 alert("禁止删除!");
27 }
28 }
29 else{
30 flag=false;
31 }
32 //判断成功后直接删除
33 if (flag) { // '/Controller/Action'
34 $.get("/Controller/Delete", { id: id }, function (data) {
35 if (data.result) {
36 window.location.href = window.location.href;
37 }
38 else {
39 alert("删除失败!");
40 }
41 })
42 }
43 }
44 </script>

Controller 层

View Code
1 //删除
2 public ActionResult Delete(int id)
3 {
4 return Json(new { result = BLL.Delete(id) }, JsonRequestBehavior.AllowGet);
5 }
6
7 //判断能否删除
8 [HttpPost]
9 public ActionResult AllowDelete(int id)
10 {
11 return Content(BLL.AllowDelete(id).ToString().ToLower());//ToLower() 转化为小写字母
12 }
posted @ 2011-05-31 16:46  烟岚  阅读(751)  评论(1编辑  收藏  举报