How to delete rows from table when clicking button

Bram

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>
atiyar

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to delete selected rows (multiple rows) from CheckboxTableViewer when button clicks? (table is connected to oracle database)

From Dev

How to delete button after clicking on "delete" button?

From Dev

How to delete some similar rows from a table?

From Dev

Adding new rows to table on clicking button in JQuery

From Dev

Delete rows from table

From Dev

How to use radio button to delete rows in the table using angularjs?

From Dev

How to delete from table then delete what deleted rows referenced? (postgresql)

From Dev

How to Use Delete Triggers to Delete Rows From Same Table?

From Dev

How to prevent page from moving down when clicking on Log in button?

From Dev

How to prevent page from moving down when clicking on Log in button?

From Dev

How to delete rows from a table based on a join with another table?

From Dev

How to delete rows from one table matching another table?

From Dev

How to reset the whole View by clicking the delete Button

From Dev

Delete rows from table when the result are not in a csv file

From Dev

Get the model when clicking a button on a table row

From Dev

Get the model when clicking a button on a table row

From Dev

AngularJS Hide rows from table on given condition when a button is clicked

From Dev

Delete item from both, database and listview on clicking Delete Button

From Dev

Delete item from both, database and listview on clicking Delete Button

From Dev

Delete X rows from table

From Dev

Can not add delete button to all rows in a table

From Dev

Delete button doesn't work in table rows

From Dev

Postgresql: How to delete rows from table constrained by date?

From Dev

How to delete unknown number of rows from sql table

From Dev

How to quickly delete a column from a table containing 600 million rows?

From Dev

How to delete many rows from frequently accessed table

From Dev

How To Delete Rows From Temp Table With EntityDelete In CFSCRIPT

From Dev

How to delete all rows from table which has no FK relation

From Dev

Postgresql: How to delete rows from table constrained by date?

Related Related

  1. 1

    how to delete selected rows (multiple rows) from CheckboxTableViewer when button clicks? (table is connected to oracle database)

  2. 2

    How to delete button after clicking on "delete" button?

  3. 3

    How to delete some similar rows from a table?

  4. 4

    Adding new rows to table on clicking button in JQuery

  5. 5

    Delete rows from table

  6. 6

    How to use radio button to delete rows in the table using angularjs?

  7. 7

    How to delete from table then delete what deleted rows referenced? (postgresql)

  8. 8

    How to Use Delete Triggers to Delete Rows From Same Table?

  9. 9

    How to prevent page from moving down when clicking on Log in button?

  10. 10

    How to prevent page from moving down when clicking on Log in button?

  11. 11

    How to delete rows from a table based on a join with another table?

  12. 12

    How to delete rows from one table matching another table?

  13. 13

    How to reset the whole View by clicking the delete Button

  14. 14

    Delete rows from table when the result are not in a csv file

  15. 15

    Get the model when clicking a button on a table row

  16. 16

    Get the model when clicking a button on a table row

  17. 17

    AngularJS Hide rows from table on given condition when a button is clicked

  18. 18

    Delete item from both, database and listview on clicking Delete Button

  19. 19

    Delete item from both, database and listview on clicking Delete Button

  20. 20

    Delete X rows from table

  21. 21

    Can not add delete button to all rows in a table

  22. 22

    Delete button doesn't work in table rows

  23. 23

    Postgresql: How to delete rows from table constrained by date?

  24. 24

    How to delete unknown number of rows from sql table

  25. 25

    How to quickly delete a column from a table containing 600 million rows?

  26. 26

    How to delete many rows from frequently accessed table

  27. 27

    How To Delete Rows From Temp Table With EntityDelete In CFSCRIPT

  28. 28

    How to delete all rows from table which has no FK relation

  29. 29

    Postgresql: How to delete rows from table constrained by date?

HotTag

Archive