Asp.net mvc 如何使用复选框删除多条记录?
我正在为我的研究所创建一个项目。使用Asp.net MVC,我需要选中复选框的多个删除选项 我在视图中添加了一个检查,但没有删除多个原始数据。我不想使用第三方插件。请帮帮我Asp.net mvc 如何使用复选框删除多条记录?,asp.net-mvc,Asp.net Mvc,我正在为我的研究所创建一个项目。使用Asp.net MVC,我需要选中复选框的多个删除选项 我在视图中添加了一个检查,但没有删除多个原始数据。我不想使用第三方插件。请帮帮我 <table class="table table-striped table-condensed table-bordered"> <tr> <th> Select </th> <th>
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Select
</th>
<th>
@Html.ActionLink("Book Id", "Index", new { sortOrder = ViewBag.IdSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Price
</th>
<th>
Category
</th>
<th class="text-center">
Photo
</th>
<th>
User
</th>
<th>Edit</th>
<th>Delete</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" value="@item.BookId" />
</td>
<td>
@Html.DisplayFor(modelItem => item.BookId)
</td>
<td>
@Html.ActionLink(item.BookTitle, "Details", new
{
id = item.BookId
})
</td>
<td>
@Html.DisplayFor(modelItem => item.PublishDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td class="text-center">
<img class="img-thumbnail" width="50" height="50" src="~/ContentImages/Full/@item.Photo" />
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.BookId })
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.BookId })
</td>
</tr>
}
</table>
挑选
@ActionLink(“图书Id”,“索引”,新{sortOrder=ViewBag.IdSortParm,currentFilter=ViewBag.currentFilter})
@ActionLink(“标题”,“索引”,新的{sortOrder=ViewBag.NameSortParm,currentFilter=ViewBag.currentFilter})
@ActionLink(“日期”,“索引”,新{sortOrder=ViewBag.DateSortParm,currentFilter=ViewBag.currentFilter})
价格
类别
照片
使用者
编辑
删除
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.BookId)
@Html.ActionLink(item.BookTitle,“详细信息”,新增)
{
id=item.BookId
})
@DisplayFor(modelItem=>item.PublishDate)
@DisplayFor(modelItem=>item.Price)
@DisplayFor(modelItem=>item.Category)
@DisplayFor(modelItem=>item.UserName)
@ActionLink(“编辑”,“编辑”,新的{id=item.BookId})
@ActionLink(“删除”,“删除”,新的{id=item.BookId})
}
首先,每个复选框需要不同的Id
,以便知道要删除的记录。其次,如果将“删除”作为链接实现,那么浏览器将执行GET
操作,而不是POST
。假设您没有使用AJAX
,那么您需要一个表单,以便POST
到处理删除的控制器操作。这可能是一种实现方法:
首先将表嵌入一个命名表单中,该表单执行批删除操作,如下所示:
@{ Html.BeginForm("BatchDelete", "Book", FormMethod.Post, new { name = "tableForm" }); }
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Select
</th>
<th>
@Html.ActionLink("Book Id", "Index", new { sortOrder = ViewBag.IdSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Title", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
Price
</th>
<th>
Category
</th>
<th class="text-center">
Photo
</th>
<th>
User
</th>
<th>Edit</th>
<th>Delete</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<input type="checkbox" name="deleteInputs" value="@item.BookId" />
</td>
<td>
@Html.DisplayFor(modelItem => item.BookId)
</td>
<td>
@Html.ActionLink(item.BookTitle, "Details", new
{
id = item.BookId
})
</td>
<td>
@Html.DisplayFor(modelItem => item.PublishDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td class="text-center">
<img class="img-thumbnail" width="50" height="50" src="~/ContentImages/Full/@item.Photo" />
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.BookId })
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.BookId })
</td>
</tr>
}
</table>
<!-- Section for buttons -->
<div class="actions">
<a href="javascript:(function(){document.tableForm.submit();return void(0);})()">
Delete selected books
</a>
</div>
@{ Html.EndForm(); }
请注意:
方法的前两个参数是操作名和控制器名(不带“controller”后缀)Html.BeginForm
- 同一方法的最后一个参数包括表单的名称。该名称在链接的javascript中使用,以指示要提交的表单
public class BookController : Controller
{
// ...
[HttpPost]
public ActionResult BatchDelete(int[] deleteInputs)
{
// You have your books IDs on the deleteInputs array
if (deleteInputs != null && deleteInputs.Length > 0)
{
// If there is any book to delete
// Perform your delete in the database or datasource HERE
}
// And finally, redirect to the action that lists the books
// (let's assume it's Index)
return RedirectToAction("Index");
}
// ...
}