我有一個從數據庫中顯示所有基礎設施(它們的名稱,描述,性能)的表。該表的最後一列有複選框。選擇一些複選框後,我想要一個按鈕,它將刪除所有選定的基礎設施。這裏有表格的代碼。通過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函數。我會很感激任何幫助。
不錯的完整答案! –