我有一个从数据库中显示所有基础设施(它们的名称,描述,性能)的表。该表的最后一列有复选框。选择一些复选框后,我想要一个按钮,它将删除所有选定的基础设施。这里有表格的代码。通过jQuery.post()方法将复选框的值发送给控制器
<table>
<tr>
<th>
Nazwa //name
</th>
<th>
Opis // description
</th>
<th>
Wydajność // performance
</th>
<th>
zaznacz // select
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nazwa)
</td>
<td>
@Html.DisplayFor(modelItem => item.Opis)
</td>
<td style="text-align: center">
@Html.DisplayFor(modelItem => item.Wydajnosc) j
</td>
<td style="padding-left: 2%; padding-right: 2%;">
<input type='checkbox' id='myCheckbox' name='Infrastuktura[@item.InfrastrukturaID].myCheckbox' value='@item.InfrastrukturaID' />
</td>
</tr>
}
我知道我需要一些变量,它会保存复选框的所有值。我有类似的东西:(但didnt't正常工作)
var checked = $('input[type="checkbox"]#myCheckbox').attr('checked');
然后,我必须把这个数据(例如使用jQuery.post()),我写了几个功能,发送与后数据,但没有一个能正常工作。下面我把这些功能之一:
$('#submitButton').click(function() {
if (checked) {
$.ajax({
type: 'POST',
url: '/Home/InfrastrukturaDelete',
data: 'id: ' +checked,
contentType: 'json',
dataType: 'json'
});
}
else {
alert("ble");
}
});
这是我在HomeController的方法:
[HttpPost]
public ActionResult InfrastrukturaDelete(int[] myCheckbox)
{
foreach (int item in myCheckbox)
{
Infrastruktury infrastruktura = naukaRepository.GetInfrastruktura(item);
naukaRepository.DeleteInfrastruktura(infrastruktura);
naukaRepository.Save();
}
return RedirectToAction("InfrastrukturyView");
}
我总是得到错误500或资源解释为其他但MIME类型不确定的转移。另外如果可以的话,告诉我控制器应该如何看待,以处理这个Ajax函数。我会很感激任何帮助。
不错的完整答案! –