在ASP.NET Core 5 MVC中,如果你想在视图中获取表格中已选中(已检查)行的ID,你可以使用JavaScript(或jQuery)来处理客户端的交互,并通过AJAX请求将数据发送到服务器。以下是一个基本的步骤指南和示例代码:
<table id="myTable">
<thead>
<tr>
<th><input type="checkbox" id="checkAll"></th>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td><input type="checkbox" class="rowCheckbox" value="@item.Id"></td>
<td>@item.Id</td>
<td>@item.Name</td>
</tr>
}
</tbody>
</table>
<button id="getCheckedIds">Get Checked IDs</button>
$(document).ready(function() {
$('#getCheckedIds').click(function() {
var checkedIds = [];
$('.rowCheckbox:checked').each(function() {
checkedIds.push($(this).val());
});
$.ajax({
url: '/YourController/GetCheckedIds',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ ids: checkedIds }),
success: function(response) {
console.log('Checked IDs:', response);
},
error: function(xhr, status, error) {
console.error('Error:', error);
}
});
});
});
[HttpPost]
public IActionResult GetCheckedIds(List<int> ids)
{
// 处理获取到的ID列表
// 例如,可以将它们保存到数据库或进行其他操作
return Json(new { success = true, data = ids });
}
通过上述步骤和代码示例,你应该能够在ASP.NET Core 5 MVC中实现获取表格中已检查行的ID的功能。
领取专属 10元无门槛券
手把手带您无忧上云