简体   繁体   中英

How Can Delete Multiple Record using CheckBox In asp.net Mvc without Entity Framework

im working with asp.net mvc and im trying to delete Multiple record using Ckeckbox, But When I select record and click on Delete button It take Id=Null . please Suggest me some solution

  1. following is my index view

    <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> }

when i run project msg :"Error while deleting the records!" show

  1. Following is my controller For Delete action

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

Sending parameter array, but coming parameter of 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);
    }

Input parameter should be string example ids:'1,2,3' and you have to passed that string paramter to Stored Procedure and sp itself you should store comma separated string to temp table or table variable and perform delete with sql queries.

Controller:

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

in JS Side:

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

i got my mistake, do following in controller Delet Aaction

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM