Click here to Skip to main content
15,888,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First time delete works correctly but when I delete another row without refreshing the page code hits controller twice the first time when it hits it deleted the row so when second time it goes on post HTTP again it gives null because data was delte in first hit.

What I have tried:

here is my controller code

[HttpGet]
       public ActionResult deleteUserDialog(int id)
       {
           var Company = db.tbl_Compnay.Where(x => x.ID == id).Select(a=> new Company{ ID = a.ID }).FirstOrDefault();
           ViewBag.UserId = id;
           return View(Company);
       }
       [HttpPost]
       public ActionResult deleteUserDialog(Company dataObject)
       {

           var Company = db.tbl_Compnay.Where(x => x.ID == dataObject.ID).FirstOrDefault();
           db.tbl_Compnay.Remove(Company);
           db.SaveChanges();

           return Json(new
           {
               Message = "Deleted Successfully."
           }, JsonRequestBehavior.AllowGet);

       }

this delete view code
@{
    Layout = null;
}

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

<div class="modal-header">
    <button type="button" style="margin-right: 10px" class="close" data-dismiss="modal"</button>
    <br />
    <h4 class="modal-title">Delete Employee</h4>
</div>


<div class="modal-body">

    @using (Html.BeginForm("Delete", "CURD", FormMethod.Post))
    {
        <p>Are you sure you want to delete this?</p>
                <div class="modal-footer">
                    <input type="submit" class="btn btn-danger" value="Delete" />

                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                </div>
    }
</div>
Posted
Updated 26-Mar-18 2:08am

In my opinion there are two solutions:
1. Change your .cshtml corresponding to the view. Add a onclick in the submit buttom, so that first disables then submit buttom and then perform then submit.
2. In your controller [POST] if there is no row when yoy find the FirstOrDefault, then simply do nothing (dont try to delete any row).
 
Share this answer
 
This is a common task - and handled via AJAX, looks very nice as the screen drops deleted records without an otherwise discernible refresh.

Now, although I describe this as though it were a web application, the basic principle is the same: each record refers to itself - so it always has a valid and correct target.

The easiest way is to have a button on each row that contains a reference unique to that row so that it can be deleted. After you respond to the row, the new list is drawn (AJAX) and that row's gone. All existing rows work the same way, sending to the server side only a reference to themselves. Since they only refer to themselves, they are (1) always correctly picked, and (2) never a non-existent record (no null possible).

Now, doing this with a single delete button is more difficult, but the same in principle. You may have a checkbox or other marker (so you can pick many rows if you like). You need to keep track of the rows select, IDEALLY, by a unique identifier that refers to the server side data. You cans send a list and operate on each item separately - and then send back the updated list (missing the deleted items). If you want to keep a list, you may be interested in a type='hidden', where you can store anything you wish as a value. Also, there's JSON.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900