c# - Deleting a Resource -
i have asp.net mvc app. need delete record. in controller, have action looks this:
[httpget] public actionresult orders() { return view(); } [httpdelete] public actionresult orders(int orderid) { return redirecttoaction("orders", new { d = "true" }); } in view, have:
<button type="button" class="btn btn-link" onclick="return deleteorder(@order.orderid);">delete order</button> function deleteorder(id) { if (confirm('are sure want delete?')) { $.ajax({ url: '/ordercontroller/orders/', type: 'delete', data: { orderid: id } }); } return false; } oddly, approach [httpdelete] orders never reached. i'm not sure how delete action work in mvc action.
probably url wrong. not need specify sufix controller on class , best way right url according route tables using url property in view.
anoth point async request (delete action method) returns redirect , should deal on client-side because jquery not redirect client. try this:
on client-side
$.ajax({ url: '@url.action("orders", "order")', type: 'delete', data: { orderid: id }, success: function(data) { if (data.success) { window.location.href = data.redirecturl; } } }); on server-side:
[httpdelete] public actionresult orders([frombody]int orderid) { // check if async request if (request.isajaxrequest) { return json(new { success = true, urlredirect = url.action("orders", new { d = "true "}); } return redirecttoaction("orders", new { d = "true" }); }
Comments
Post a Comment