[英]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.