【问题标题】:How Can Delete Multiple Record using CheckBox In asp.net Mvc without Entity Framework如何在没有实体框架的 asp.net Mvc 中使用 CheckBox 删除多条记录
【发布时间】:2020-01-27 12:39:30
【问题描述】:

我正在使用 asp.net mvc,我正在尝试使用 Ckeckbox 删除多条记录,但是当我选择记录并单击删除按钮时,它需要 Id=Null 。 请给我一些解决方案

  1. 以下是我的索引视图

    <h2>Index</h2>
    <input type="button" id="Delete"
           value="Delete Selected Employee" />
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    @using (Html.BeginForm("BatchDelete", "Employee", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <table class="table">
            <tr>
                <th><input type="checkbox" id="checkAll" "All"</th>
                <th>
                    @Html.DisplayNameFor(Model => Model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.FirstName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.LastName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Gender)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.DOB)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Hobby)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Photo)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.City)
    
                </th>
                <th></th>
            </tr>
    
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        <input type="checkbox" class="checkBox"
                               value="@item.Id" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.FirstName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.LastName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Gender)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DOB)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Hobby)
                    </td>
                    <td>
                        <img width="50" height="50" src="@item.Photo" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.City)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                        @Html.ActionLink("Detail", "Detail", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                    </td>
                </tr>
            }
        </table>
        <script>
            $(document).ready(function () {
    
                $("#checkAll").click(function () {
                    $(".checkBox").prop('checked',
                        $(this).prop('checked'));
                });
    
                $("#Delete").click(function () {
                    var selectedIDs = new Array();
                    $('input:checkbox.checkBox').each(function () {
                        if ($(this).prop('checked')) {
                            selectedIDs.push($(this).val());
                        }
                    });
    
                    var options = {};
                    options.url = "/Employee/Delete";
                    options.type = "POST";
                    options.data = JSON.stringify(selectedIDs);
                    options.contentType = "application/json";
                    options.dataType = "json";
                    options.success = function (msg) {
                        alert(msg);
                    };
                    options.error = function () {
                        alert("Error while deleting the records!");
                    };
                    $.ajax(options);
    
                });
            });
        </script>
    
    }
    

当我运行项目消息时:“删除记录时出错!”显示

  1. 以下是我的删除操作控制器

    [HttpGet]
            public ActionResult Delete(int? Id)
            {
                if (Id == null)
                {
                    return View();
                }
                Employee emp = objemployee.GetEmployeeData(Id);
                if (emp == null)
                {
                    return View();
                }
                return View(emp);
            }
    
            [HttpPost, ActionName("Delete")]
            public ActionResult DeleteConfirmed(int? Id)
            {
                objemployee.DeleteEmployee(Id);
                return RedirectToAction("Index");
            }
    

【问题讨论】:

    标签: c# json ajax asp.net-mvc


    【解决方案1】:

    发送参数数组,但传入action int参数...

     [HttpGet]
        public ActionResult Delete(int[] selectedIDs)
        {
            if (Id == null)
            {
                return View();
            }
            Employee emp = objemployee.GetEmployeeData(Id);
            if (emp == null)
            {
                return View();
            }
            return View(emp);
        }
    

    【讨论】:

    • 这样做.........public ActionResult DeleteConfirmed(int?Id, int[] selectedIDs) { if(Id>0) { objemployee.DeleteEmployee(Id); } else { foreach(int item in selectedIDs) { objemployee.DeleteEmployee(item); } }
    【解决方案2】:

    输入参数应该是字符串示例 ids:'1,2,3' 并且您必须将该字符串参数传递给存储过程和 sp 本身您应该将逗号分隔的字符串存储到临时表或表变量并使用 sql 查询执行删除.

    控制器:

     public ActionResult Delete(string selectedIDs)
            {
                if (selectedIDs == null)
                {
                    return View();
                }
                Employee emp = objemployee.GetEmployeeData(selectedIDs);
                if (emp == null)
                {
                    return View();
                }
                return View(emp);
            }
    

    在JS端:

    options.data = { selectedIDs: selectedIDs.toString()};
    

    【讨论】:

      【解决方案3】:

      我弄错了,在控制器删除动作中执行以下操作

       [HttpPost, ActionName("Delete")]
              public ActionResult DeleteConfirmed(int? Id, int[] selectedIDs)
              {
                  if(Id>0)
                  {
                      objemployee.DeleteEmployee(Id);
      
                  }
                  else
                  {
                      foreach(int item in selectedIDs)
                      {
                          objemployee.DeleteEmployee(item);
                      }
                  }
                  return RedirectToAction("Index");
              } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2010-12-17
        • 2011-12-06
        相关资源
        最近更新 更多