如何使用asp.net mvc从tableview中删除所选记录

时间:2017-05-14 18:51:55

标签: javascript asp.net-mvc c#-4.0 asp.net-ajax

这是我的视图代码编辑按钮成功运行。我想删除特定的选定记录。当您的点击删除图标模型弹出窗口打开时,它会询问是或否。如果用户单击是,我需要调用操作方法。如何调用该方法?当我尝试运行参数null时,它将采取行动方法。

  <table id="mytable" class="table table-bordred table-striped">
                <thead>
                    <tr>
                        <th>User ID</th>
                        <th>User Name</th>
                        <th>Email</th>
                        <th>Edit</th>
                        <th>Delete</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var gt in Model.UserList)
                    {
                        <tr>
                            <td>@gt.UserId</td>
                            <td>@gt.UserName</td>
                            <td>@gt.Email</td>
                            @using (Ajax.BeginForm("getUserDetailById", new AjaxOptions() { UpdateTargetId = "Edit-User", AllowCache = true, InsertionMode = InsertionMode.ReplaceWith,LoadingElementId="resultLoadingDiv" }))
                            {
                                @Html.Hidden("userId", @gt.UserId)

                                <td><p data-placement="top" data-toggle="tooltip" title="Edit/View"><button class="btn btn-primary btn-xs" data-title="Edit/View" data-toggle="modal" data-target="#edit"><span class="glyphicon glyphicon-pencil"></span></button></p></td>
                            }    
                                <td><button class="btn btn-danger btn-xs delete"  data-userid="@gt.UserId" data-title="Delete"><span class="glyphicon glyphicon-trash"></span></button></td>
                        </tr>
                    }
                </tbody>
            </table>

脚本:

        debugger;
    $('#mytable').on('click', '.delete', function () {
    var userId = $(this).data('UserId');
   if (bootbox.confirm('Do you want to delete this item?', function (result) 
    {
    if (result == true) {
        $.ajax({
    type: "POST",
    url: "Admin/deleteUser/" + userId,
    success: function (result) {
      },
     complete: function () {

  },
    error: function (xhr, status, errorThrown) {
  }
  });
  }
  else {
        return;
    }
   }));
  })

我的控制器:

 [HttpPost]
    public JsonResult deleteUser(string userId)
    {
        _objuser = new UserService();
        var  status = true;


        var gt = _objuser.deleteUserDetail(userId);


        return new JsonResult { Data = status, JsonRequestBehavior = 
        JsonRequestBehavior.AllowGet };
        }

可能只是控制器操作的问题。

1 个答案:

答案 0 :(得分:1)

最好下载并使用Bootbox在您的应用中提供一致的提醒框,并且它也易于使用:

bootbox.confirm({
message: "This is a confirm with custom button text and color! Do you like it?",
buttons: {
    confirm: {
        label: 'Yes',
        className: 'btn-success'
    },
    cancel: {
        label: 'No',
        className: 'btn-danger'
    }
},
callback: function (result) {
 if(result === true){
     //YOUR DELETE ACTION METHOD AJAX CALL HERE
  }
 }
});

将此类删除事件附加到表格删除按钮

$('#yourTableId').on('click', '.delete', function () {           
        var userId= $(this).data('userid');
        if (bootbox.confirm('Do you want to delete this item?', function (result) {
            if (result == true) {
                $.ajax({
                    type: "POST",
                    url: "Admin/deleteUser", 
                    data:JSON.stringify({userId:userId})
                    contentType: 'application/json; charset=utf-8',                       
                    success: function (result) {
                    },
                    complete: function () {

                    },
                    error: function (xhr, status, errorThrown) {
                    }
                });
            }
            else {
                return;
            }
        }));

在视图中更改删除按钮,如下所示:

<button class="btn btn-danger btn-xs delete" data-userid="@gt.UserId" data-title="Delete"><span class="glyphicon glyphicon-trash"></span></button>