I'm working on my first Asp.Net Core application, and am struggling with modals, Razor pages and route mapping are completely new things for me, and javascript is a pretty old thing for me. I'm trying to create a delete modal that can be used for any object (my test object is of type Employee). Maybe this isn't possible, I'm not sure. My modal displays fine, but when I click to call my DeletePOST method I get a 405 error. The URL in question returns as https://localhost:44313/Employee/DeletePOST/1 (when the employee with id = 1 is selected). The "warning" message that I get in the console is (index):6789 crbug/1173575, non-JS module files deprecated.(anonymous) @ (index):6789.
Here is the applicable code from View.Employee.Index
<tbody>
@foreach (var employee in Model)
{
<tr id="[email protected]">
<td>@employee.Name</td>
<td>@employee.Phone</td>
<td>@employee.Email</td>
<td>@employee.Address</td>
<td>@employee.Role</td>
<td>@employee.Availability</td>
<td role="group">
<a asp-controller="Employee" asp-action="Edit" asp-route-id="@employee.Id"
> <i ></i> Edit</a>
<!--delete modal confirmation button-->
<a id="#delete" data-id="@employee.Id"
data-url ="@Url.Action("DeletePOST","Employee")/"
data-body-message= "Are you sure you want to delete this employee?">
<i ></i> Delete</a>
</td>
</tr>
}
</tbody>
Code from wwwroot.js.delete.js
$((function () {
var target;
var pathToDelete;
var id;
$('body').append(`
<div id="deleteModal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
<div role="document">
<div >
<div >
<button type="button" data-bs-dismiss="modal"
aria-label="Close"><span aria-hidden="true">×</span> </button>
<h4 id="myModalLabel">Warning</h4>
</div>
<div >
</div>
<div >
<button type="button" data-dismiss="modal"
id="cancel-delete">Cancel</button>
<button type="submit"
id="confirm-delete">Delete</button>
</div>
</div>
</div>
</div>`);
//Delete Action
$(".delete").on('click', (e) => {
e.preventDefault();
target = e.target;
id = $(target).data('id');
pathToDelete = $(target).data('url');
var bodyMessage = $(target).data('body-message');
pathToDelete = pathToDelete id;
$(".delete-modal-body").text(bodyMessage);
$("#deleteModal").modal('show');
});
$("#confirm-delete").on('click', () => {
window.location.href = pathToDelete; //suspect issue
});
}()));
Code from Controllers.EmployeeController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePOST(int? id)
{
var selectedEmployee = _db.Employees.Find(id);
if (selectedEmployee == null)
{
return NotFound();
}
_db.Employees.Remove(selectedEmployee);
_db.SaveChanges();
return RedirectToAction("Index"); //suspect issue
}
```
CodePudding user response:
If you want to call DeletePOST in js,you can try to use ajax,since you need to add AntiForgeryToken to the request,you also need to add @Html.AntiForgoryToken
to your view:
view:
<tbody>
@foreach (var employee in Model)
{
<tr id="[email protected]">
<td>@employee.Name</td>
<td>@employee.Phone</td>
<td>@employee.Email</td>
<td>@employee.Address</td>
<td>@employee.Role</td>
<td>@employee.Availability</td>
<td role="group">
<a asp-controller="Employee" asp-action="Edit" asp-route-id="@employee.Id"
> <i ></i> Edit</a>
<!--delete modal confirmation button-->
<a id="#delete" data-id="@employee.Id"
data-url ="@Url.Action("DeletePOST","Employee")/"
data-body-message= "Are you sure you want to delete this employee?">
<i ></i> Delete</a>
</td>
</tr>
}
</tbody>
@Html.AntiForgoryToken
js:
$("#confirm-delete").on('click', () => {
$.ajax({
type: "POST",
url: pathToDelete,
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
}).done(function (result) {
//redirect to Index here
window.location.href="Index";
});
});
Or you can remove the following code in Controller:
[HttpPost]
[ValidateAntiForgeryToken]