3

My code delete the whole table due to usage of each function. I want to delete multiple rows by clicking each delete image link in cell which is mentioned in my view.

If I do not use each function it only delete the first row on which I have clicked and after that if I want to delete another row I have to refresh the page.

my controller

[HttpPost]

    public ActionResult Delete(int? id)
    {
        using (var db = new DAL.InventoryDBEntities())
        {

            DAL.DetailsTable personalDetail = db.DetailsTable.Find(id);
            db.DetailsTable.Remove(personalDetail);
            db.SaveChanges();
            return Json(true ,JsonRequestBehavior.AllowGet);    
        }

    }

my script

function Delete() {


    $("a.deleteimage").each(function () {
        var Id = $(this).data("model-id");
        var url = "/Home/Delete/" + Id;
        $.ajax({
            url: url,
            type: "POST",

            success: function () {

                $("#tblDetails").load(window.location + " #tblDetails");
            },

            error: function () {
                alert("Fails");
            }

        });

    });

};

my razor view

 <table id="tblDetails" class=" table-bordered table-striped table-condensed">
    <thead>
        <tr>
            <th>Name</th>
            <th>City</th>
            <th>Country</th>
            <th>Options</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
        <tr>
            <td>@item.Name</td>
            <td>@item.City</td>
            <td>@item.Country</td>
            <td>
            <a id="editimage" href="javascript:;">
                     <img id="image" src="/UserImages/edit.png" />
            </a><a class="deleteimage" data-model-id="@item.Id">
                     <img src="/UserImages/delete.png" />
            </a>
            </td>

        </tr>
        }
    </tbody>

</table>

1 Answer 1

3

What I have concluded from your words you want to delete each row without refreshing your page, its pretty simple it does not require each function and I have done it without using it.

Your Controller looks fine and you do not have any need to change it. What I suggest you to update your Script and your Razor View.

First of all you have to update your script If you have your delete function inside of DOM place out of it whenever you call a function separately by jquery event method it is considered to be good practice to place your whole function outside of DOM and do the following changes in your code.

Updated Script:

function Delete(obj) {
var ele = $(obj);
var Id = ele.data("model-id");
var url = "/Home/Delete/" + Id;
$.ajax({
    url: url,
    type: "POST",

    success: function () {

        ele.closest("tr").remove();
       },

    error: function () {
        alert("Fails");
    }

});

};

Update this Button in your Razor View:

<a class="delete" data-model-id="@item.Id" onclick="Delete(this)" >.........</a>

I hope this will resolve your problem

Best regards;

Ibrahim Inam

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.