Javascript 如何使用asp.net mvc从tableview中删除所选记录
这是“我的视图代码”编辑按钮成功运行。我想删除所选的特定记录。当您单击删除图标模型弹出窗口将打开,然后它将询问是或否。如果用户单击是,我需要调用操作方法。如何调用该方法?当我尝试运行参数null时,它的coming to action方法Javascript 如何使用asp.net mvc从tableview中删除所选记录,javascript,asp.net-mvc,c#-4.0,asp.net-ajax,Javascript,Asp.net Mvc,C# 4.0,Asp.net Ajax,这是“我的视图代码”编辑按钮成功运行。我想删除所选的特定记录。当您单击删除图标模型弹出窗口将打开,然后它将询问是或否。如果用户单击是,我需要调用操作方法。如何调用该方法?当我尝试运行参数null时,它的coming to action方法 <table id="mytable" class="table table-bordred table-striped"> <thead> <tr>
<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>
我的控制器:
[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 };
}
可能只是控制器操作的问题。最好下载并在应用程序中使用一致的警报框,而且使用起来也很方便:
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>
没有问题。如果对你有效,请将其标记为答案。谢谢。嗨,伙计,当我点击表格的第一行时,模型弹出窗口就来了,但若我点击第二行,它就不会来了。我的表行将动态生成如何基于ID获取?你能帮我吗?老兄,我有更新的工作代码。哪个是你送的那个。modelpopup窗口即将出现操作方法也被成功调用,但唯一的问题是用户id即将出现空值。我的用户id是字符串示例:sena023。在ajax中从删除url中删除parseInt。更新答案。我已添加控制器代码。一切正常。当我选中alert(userId)时,它将显示选中的行userId。唯一的问题是我得到的是空值。一旦我删除我必须重新加载页面,并应显示成功的消息给用户。我已经这样修改了我的ajax代码。$。ajax({type:“POST”,url:'/Admin/deleteUser/',数据:JSON.stringify(userId),contentType:'application/JSON',success:function(result){
<button class="btn btn-danger btn-xs delete" data-userid="@gt.UserId" data-title="Delete"><span class="glyphicon glyphicon-trash"></span></button>