I'm wondering how I can delete all rows from my Alerts table in the database. I'm using ASP.NET Core in combination with Entity Framework. When I click the delete button right now I'll get an Error 404 (not found)
Any help would be greatly appreciated!
Controller
private readonly FrontlineDbContext _dbContext;
public class AlertsController : Controller{
public AlertsController( FrontlineDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpPost]
[Authorize(Roles = "Admin")]
public void Delete()
{
var alertsList = _dbContext.Alerts.ToList();
_dbContext.Alerts.RemoveRange(alertsList);
_dbContext.SaveChanges();
//displays a success message after updating, as stated in the Shared>_Layout
TempData["message"] = $"Alerts have been deleted";
}
}
AlertsView
<h1>Alerts Overview</h1>
<a asp-action="Delete"> Delete Alerts</a>
Your view code is generating an GET request which is not reaching the Delete
action method, because it is marked with [HttpPost]
attribute.
To generate a POST request, and reach your Delete
method, modify your view code as -
<form asp-action="Delete">
<input type="submit" value="Delete" class="btn btn-link"/>
</form>
Also, replace [HttpPost]
with [HttpPost, ActionName("Delete")]
on the action method, just to be sure.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments