[英]Delete btn in MVC.net
我可能需要額外的眼睛,但我的刪除btn無法正常工作,它確實會返回一條消息,但是單擊“是”或“確定”后,它不會刪除我想要刪除的數據,基本上什么也沒發生,我認為清單存在問題.id部分。 謝謝,我知道這對其他用戶不是一個好問題,但我感謝您的幫助。
<tbody>
@foreach (var inventory in Model)
{
<tr>
<td>@Html.ActionLink(inventory.PartNumber, "Edit", "Inventory", new
{ id = inventory.Id }, null)</td>
<td>@inventory.PinNumber</td>
<td>@inventory.PartQuantity </td>
<td>@inventory.PartPrice </td>
<td>@inventory.PartDescrption</td>
<td> <button data-inventory-id="@inventory.Id" class="btn-link js-delete">Delete</button> </td>
</tr>
}
</tbody>
</table>
@section scripts
{
<script>
$(document).ready(function ()
{
$("#inventories").DataTable();
$("#inventories .js-delete").on("click", function () {
var button = $(this);
if (confirm("Are you sure you want to delete this Part Number?")) {
$.ajax({
url: "/inventory/" + button.attr("data-inventory-id"),
method: "DELETE",
success: function () {
button.parents("tr").remove();
}
});
}
});
});
</script>
}
這是我的Delete Action控制器:
[HttpDelete]
public void DeleteInventory(int id)
{
var inventoryInDb = _context.Inventory.SingleOrDefault(c => c.Id == id);
_context.Inventory.Remove(inventoryInDb);
_context.SaveChanges();
}
我在教程中沒有API,我在關注他有一個API,但我沒有創建一個。 我正在努力解決這個問題。 謝謝。
使用POST代替DELETE作為您的Ajax方法怎么樣? 或者簡單地使用$ .post方法。
您很可能沒有在后端API中創建DELETE方法。 要確定原因,請打開Chrome的開發人員工具(確保您位於控制台標簽上),然后單擊按鈕。 您將看到一條錯誤消息,指出“未找到方法DELETE”或類似的消息。
如果顯示“不允許使用方法”,則與權限有關(單擊按鈕的用戶無權訪問該API)。
In controller:
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Inventory inventory = _context.Inventory.Find(id);
if (inventory == null)
{
return HttpNotFound();
}
return View(inventory);
}
in index add delete btn, this is an ajax call to the delete btn its used with dataTables to render data faster..
{
"render": function (data, type, row, meta) {
return '<a class="btn btn-danger"
href ="@Url.Action("Delete", "Inventory")/' + row.Id + '">Delete</a>';
}
創建一個刪除視圖:
@model InventoryTracker.Models.Inventory //this has to be re-named to your naming
convention.
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-primary" />
@Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-primary" })
</div>
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.