Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Problem to send ajax request to delete item (Returns not found!).

$
0
0

Hi

in my form, i have a list of orders which user can click on delete button to delete each order of specific person. here is my orders list :

@model IEnumerable<Orders><input type="hidden" id="hdnPersonName" value="@ViewData["_personName"]" /><button type="button" id="btnNew" class="btn btn-success btn-sm" onclick="newOrderItem(@((int)ViewData["_personID"]))">Create new</button><br /><table class="table table-bordered"><tr><th>Order ID</th><th>Order Number</th><th>Order Date</th><th>Commands</th></tr>
    @if (Model != null)
    {
        foreach (var item in Model)
        {<tr><td>@item.OrderId</td><td>@item.OrderNumber</td><td>@item.OrderDate.Value.ToShamsiDate()</td><td><input type="button" id="btnEdit" class="btn btn-warning btn-sm" onclick="editOrderItem(@item.OrderId)" value="Edit" /><input type="button" id="btnDelete" class="btn btn-danger btn-sm" onclick="deleteOrderItem(@item.OrderId)" value="Delete" /></td></tr>
        }
    }</table><div id="divPopup"></div>

As you can see in above code, The btnDelete button call 'deleteOrderItem' javascript to delete order as follow :

function deleteOrderItem(orderID) {
    debugger;
    if (confirm('Are you sure want to delete this item?')) {$.ajax({
            type: "POST",
            url: "@Url.Action('DeleteOrder','Home')",
            contentType: "application/json; charset=utf-8",
            data: "{ 'id': '"+orderID+"' }",
            success: function () {
                setTimeout(function () {
                    debugger;
                    var personID = $('#hdnPersonID').val();
                    var personName = $('#hdnPersonName').val();
                    viewItem(personID, personName);
                }, 500)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }
}

And here is my action code method to delete order from database :

/ POST: Orders/Delete/5
        public async Task<IActionResult> DeleteOrder(int id)
        {
            var orders = await _dbContext.Orders.FindAsync(id);
            _dbContext.Orders.Remove(orders);
            await _dbContext.SaveChangesAsync();
            return Content(id.ToString());
        }

But at runTime it does not works. when i run on debugging mode (set breakPoint in DeleteOrder action method) it does not fire & i think it can not find my action (or something else).

Can anybody help me where is my problem & how to solve it?

Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles