使用一个封装的json删除方法

<!-- 前台js代码:其实现的目的:利用异步的封装方法实现增删改操作!-->

 

<script type="text/javascript">

                                                                          
    function ajaxRequest(params) {
        var randnum = Math.random();
        var ajaxurl = params.url;
        var ifasync = params.ifasyncinre;
        if (ifasync != true || ifasync != false) {
            ifasync = false;
        }
        var re = null;
        var method = params.method;
        if (method == null || method == "")
            method = "POST";
        var data = params.data;
        if (data == null || data == "")
            data = "";                                                     
        var datatype = params.datatype;
        if (datatype == null || datatype == "")
            datatype = "json";
        $.ajax({
            type: method,
            url: ajaxurl,
            async: ifasync,
            data: data,
            dataType: datatype,
            success: function (result) {
                re = result;
            }
        });

        return re;
    }

    $(".delete").live("click", function () {

        if (confirm("您确定要删除吗") == false) {

            return false;

        }
        debugger;
        var Fid = $(this).attr("Fid");

        var params = { url: '/Personal/Space/Favorite/Delete',
            data: { id: Fid }
        };

        var result = ajaxRequest(params);
        if (result.Data == "1") {
            //            alert(result.Message);
            location.href("/Personal/Space/Favorite/Index");
        }
        else if (result.Data == "0") {
            alert(result.Message);
        } else {
            alert(result.Message);
        }

    })



</script>

 

页面循环读取代码:

 

 <table style="width: 80%; margin: auto; background-color:yellowgreen  ;">
                        <tr class="imgbg">
                            <!---      --->
                            <td>
                              标题
                            </td>
                            <td>
                               类别
                            </td>
                            <td>
                               操作
                            </td>
                        </tr>
          @foreach (UserFavorite p in ViewBag.DataList as List<UserFavorite>)


{ 
<tr style="border-style:solid;border-color:Red; background-color:ivory  ">
   <td>【@p.UserFavoTopic.FName】@p.FTitle</td>  <td>***【@p.UserFavoTopic.FName】***</td> <td>  @Html.ActionLink("删除", "Index", null, new { @id = "delete", @class = "delete", @Fid = p.FId, @style = "color:#669999" }) </td>
</tr>

}

                    </table>

 

后台代码:

        public ActionResult Delete(string id)
        {
            ////////////////////////////////////
            if (id != null)
                {
                    if (ufb.Delete(id))
                    {
                        return Json(new TipMessage { IsError = false, Message = "删除成功", Data = 1 });

                    }
                    else
                    {
                        return Json(new TipMessage { IsError = false, Message = "删除失败", Data = 0 });

                    }

                }
                else
                {
                    return Json(new TipMessage { IsError = false, Message = "删除失败", Data = 0 });

                }
           // return View(Model);
        }

 

 

posted @ 2013-08-05 10:14  代号 11  阅读(266)  评论(0编辑  收藏  举报