我正在使用GridMvc列出我的表,我想同时删除多个数据。我尝试了使用FormCollection的复选框,但是由于FormCollection每次都为null,所以它不起作用。如果看到错误,可以解释一下还是删除多个数据该怎么办?
在视图中删除所选按钮:
@using (Html.BeginForm("SelectedDelete", "Admin", FormMethod.Post))
{<button type="submit" class="btn btn-primary">Delete Selected</button> }
视图中的表格:
@Html.Grid(Model).Columns(column =>
{
column.Add()
.Encoded(false)
.Sanitized(false)
.Titled("Seç")
.SetWidth(30)
.RenderValueAs(x => Html.CheckBox("checked", false, new { name = "selectedID", value = x.ID }));
column.Add(m => m.FirstName).Titled("Adı").Sortable(true).Filterable(true);
column.Add(m => m.LastName).Titled("Soyadı").Sortable(true).Filterable(true);
column.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(20)
.RenderValueAs(x => Html.ActionLink(" ", "DeleteTalks", "Admin", new { id = x.ID }, new { @class = "btn btn-danger far fa-trash-alt" }));
})
删除动作:
public ActionResult SelectedDelete(FormCollection form)
{
var selectedIds = form.GetValues("selectedID");
if (selectedIds != null)
{
foreach (var id in selectedIds)
{
var sil = db.TblInformaticTalks.FirstOrDefault(x => x.ID == Convert.ToInt32(id));
db.TblInformaticTalks.Remove(sil);
db.SaveChanges();
}
return RedirectToAction("IndexTalks");
}
else
{
return HttpNotFound();
}
}
谢谢。