View层
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 >
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 层
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
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 }