• Asp.net MVC 批量删除数据


    ProductList视图

    <div class="mid">
         <div id="editInfo">
            @using (Html.BeginForm("Delete", "Admin"))
            {
               <div class="c1 headFont">
               <input type="checkbox" id="ckbSelAll" onclick="SelectAll(this.checked)" />
               <input type="submit"value="多选删除" onclick="return confirm('确定要删除吗?')" />
               </div>
               <div class="c3 headFont">产品名称</div>
               <div class="c4 headFont">发布时间</div>
               <div class="c5 headFont">编辑</div>
               <ul id="info">
                  @foreach (var p in Model)
                  {
                     <li>
                        <div class="c1 list">@Html.CheckBox("checkitem", new {value=p.ID }) 
                                       @Html.ActionLink("删除", "Delete", new { id = p.ID }, new { onclick = "return confirm('确定删除吗?')" })
                        </div>
                        <div class="c3 list">@Html.DisplayFor(modelItem => p.productName)</div>
                        <div class="c4 list">@Html.DisplayFor(modelItem => p.inDate,"LongDateTime")</div>
                        <div class="c5 list">@Html.ActionLink("编辑", "EditProduct", new { id=p.ID })</div>
                    </li>
                }
              </ul>
         }
        </div>
    </div>
    

    效果图如下:

    Controller代码:

    public ActionResult Delete(int id)
    {
       ProductInfo pro = db.ProductInfoes.Find(id);
       db.ProductInfoes.Remove(pro);
       db.SaveChanges();
       return RedirectToAction("ProductList");
    }
    [HttpPost] public ActionResult Delete(FormCollection collection) { string str = collection["checkitem"];// checkitem复选框的名 string[] strDelete = str.Split(','); foreach (var i in strDelete) { if (i != "false") { ProductInfo pro = db.ProductInfoes.Find(int.Parse(i)); db.ProductInfoes.Remove(pro); db.SaveChanges(); } } return RedirectToAction("ProductList"); }

      

  • 相关阅读:
    leetcode 1. Two Sum
    leetcode 168. Excel Sheet Column Title
    [LeetCode] Water and Jug Problem 水罐问题
    leetcode 80 Remove Duplicates from Sorted Array II
    leetcode 239. Sliding Window Maximum
    文件处理
    python网络编程 之 twisted
    ICMP & ping & traceroute
    Java String 转整形
    Java 字符数字得到整数
  • 原文地址:https://www.cnblogs.com/qk2014/p/3777744.html
Copyright © 2020-2023  润新知