0

I have a small problem in my code.. I'm trying to delete a row from database, but without using DataTables (in asp.net mvc) and it doesnt work. I've displayed every item from the database on a page in div's.. It looks like this

Index:
index

Default Edit/Delete buttons

<input type="button" value="Edit" class="btn btn-warning btn-sm mt-auto" onclick="location.href='@Url.Action("Edit", "Products", new { id = item.Id })'" />
<input type="button" value="Delete" class="btn btn-danger btn-sm mt-auto" onclick="location.href='@Url.Action("Delete", "Products", new { id = item.Id })'" />

with default button I would have to go to the Delete View and press Delete again,and I dont want that.. thats why I'm using Bootbox with Ajax

So I deleted that Delete button and added this:

<button data-product-id="@item.Id" class="btn btn-danger btn-sm mt-auto js-delete">Delete</button>

This is my controller

// GET: Products/Delete/5
    public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Product product = db.Products.Find(id);
        if (product == null)
        {
            return HttpNotFound();
        }
        return View(product);
    }

    // POST: Products/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Product product = db.Products.Find(id);
        db.Products.Remove(product);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

and this is my script that I'm using

        $(document).ready(function () {
        $(".products-panel .js-delete").on("click", function () {
            var button = $(this);
            bootbox.confirm("Do you want to delete this product", function (result) {
                if (result) {
                    $.ajax({
                        url: "/Products/Delete/" + button.attr("data-product-id"),
                        method: "GET",
                        success: function () {
                            $(button).parent().parent().remove();
                        },
                        error: function (err) {
                            console.log(err);
                        }
                    });
                }
            });
        });
    });

Currently it only removes the div of that item until you refresh the page.. (it doesnt remove it from the database).. Is there any way that I can fix this

If someone needs more photo's I can provide them..

1
  • Put a breakpoint in your code and check it. Is it even being called? Commented Aug 6, 2022 at 16:13

1 Answer 1

1

I fixed the problem, I just had to remove the GET Delete action from the Controller and Change the name on the Post method from DeleteConfirmed to Delete

Previous:

// GET: Products/Delete/5
public ActionResult Delete(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Product product = db.Products.Find(id);
    if (product == null)
    {
        return HttpNotFound();
    }
    return View(product);
}

// POST: Products/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
    Product product = db.Products.Find(id);
    db.Products.Remove(product);
    db.SaveChanges();
    return RedirectToAction("Index");
}

Working:

    // POST: Products/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(int id)
    {
        Product product = db.Products.Find(id);
        db.Products.Remove(product);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

Thanks for the people that tried to help

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.