2014-06-25 60 views
-1

我正在为我的研究所创建一个项目。使用Asp.net MVC,我需要选中复选框的多个删除选项。如何使用复选框删除多条记录?

我在我的视图中添加了一张支票,但未删除多张Raw。我不想使用第三方插件。请帮帮我。

<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> 
+0

到目前为止您尝试了什么?与我们分享您的代码。如果您不显示任何您的努力,我们很难为您提供帮助 – DevelopmentIsMyPassion

+0

我已更新查看代码 –

+0

因此,您想要删除以复选框标记的行吗? – DevelopmentIsMyPassion

回答

3

这可能是一种方法来实现它:

一是嵌入了一个名为表单中的表,执行批量删除的行为,就像这样:

@{ 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(); } 

注意到,该表后,前结束表格,我添加了一个链接,执行表单的提交。现在

,在控制器侧,asumming其名称是 “BookController的”:

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"); 
    } 

    // ... 
} 

注意:

  • 前两个参数的Html.BeginForm方法,是操作名称和控制器名称(没有“控制器”后缀)。
  • 同一方法的最后一个参数包括表单的名称。该名称用于链接的JavaScript中,以表明您要提交哪个表单。
+0

工程就像一个魅力! –

-1

首先,你需要为每个复选框不同Id,所以你知道哪个记录将被删除。其次,如果您实施“删除”作为链接,则浏览器将执行GET操作,而不是POST。假设你没有使用AJAX,那么你将需要一个表单,所以你可以POST到处理删除的控制器动作。