如何使用jquery从asp.net MVC中的数据库中删除已检查的表行 这是我的代码,请帮助我。我已经尝试了但如果任何人有建议,这不起作用请给我一些。
function saveOrder(data) {
return $.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: "/Bill/SaveOrder",
data: data,
success: function (result) {
alert(result);
location.reload();
},
error: function () {
alert("Error!")
}
});
}
var selectedValues = [];
$.each($("#empsToDel"), function () {
selectedValues.push({
ID = $("input:checked").val()
})
});
var data = JSON.stringify({
Delete:selectedValues
});
<table>
<tr>
<th></th>
<th>@Html.DisplayNameFor(model => model.Product.ProductName)</th>
</tr>
@foreach (var item in Model)
{
<tr id="new">
<td><input type="checkbox" class="checkbox" value="@item.ID" name="empsToDel" id="empsToDel" /></td>
<td>@Html.DisplayFor(modelItem => item.Product.ProductName)</td>
</tr>
}
</table>
<input id="saveOrder" type="submit" class="btn btn-default" value="ok" />
答案 0 :(得分:0)
<table>
<tr>
<th></th>
<th>@Html.DisplayNameFor(model => model.Product.ProductName)</th>
</tr>
@foreach (var item in Model)
{
<tr id="new'@item.ID'">
<td><input type="checkbox" class="checkbox" value="@item.ID"
name="empsToDel" id="empsToDel'@item.ID'" /></td>
<td>@Html.DisplayFor(modelItem => item.Product.ProductName)</td>
</tr>
}
</table>
<input id="saveOrder" type="submit" class="btn btn-default" value="ok" />
$("#saveOrder").click(function ()
{
var selectedValues = new Array();
$(".checkbox:checked").each(function () {
selectedValues.push($(this).val());
});
var clientIdList = selectedValues.join();
$.ajax(
{
type: "POST",
url: '@Url.Action("SaveOrder", "Bill")',
data: { clientIdList: clientIdList },
beforeSend: function () {
},
success: function (response) {
alert(result);
location.reload();
},
complete: function () {
},
error: function () {}
});
});
BillController.cs
public ActionResult SaveOrder( string clientIdList = null)
{
if (clientIdList != "")
{
var clientIds = clientIdList.Split(',').Select(x =>
Int64.Parse(x)).ToArray();
}
}
希望这对你有所帮助。