尝试使用部分视图更新表数据而不重新加载。为什么在控制器操作后我的表没有更新?
在我的部分视图中 _usersTable :
@model IEnumerable<Task1.Models.ApplicationUser>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Email)
@Html.CheckBoxFor(modelItem => item.userCheck, new { id = item.Id })
</td>
</tr>
}
在我的主视图中:
<table id="myTable">
@Html.Partial("_usersTable")
</table>
<script>
function deleteRecords() {
var selected = [];
$('#myTable input:checked').each(function () {
selected.push($(this).attr('id'));
});
$.ajax({
url: '/Home/DeleteRecords',
type: 'POST',
traditional: true,
data: { array: selected }
});
}
</script>
控制器动作:
[HttpPost]
public IActionResult DeleteRecords(string[] array)
{
using (var context = new ApplicationDbContext(getContextOptions()))
{
for (int i = 0; i < array.Count(); i++)
{
ApplicationUser user = context.Users.Find(array[i]);
context.Users.Remove(user);
}
context.SaveChanges();
return PartialView("_usersTable", context.Users.ToList());
}
}