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>
question from:https://stackoverflow.com/questions/65910991/how-to-delete-rows-from-table-when-clicking-button