Home > OS >  How to pass the value of the button id to backend for a post request?
How to pass the value of the button id to backend for a post request?

Time:09-17

Frontend is HTML, backend is C#, this is asp.net.

In frontend I have multiple "Delete" buttons but they have different ids, like "Delete-1", "Delete-2", etc.

In the backend I have an OnPostDelete() that does an API request, which needs the id number (the 1 and the 2) to identify what to delete.

So how do I detect which delete button was pressed, to delete the corresponding thing?

CodePudding user response:

I think you have a list or a table in your page and the Delete buttons are related to listed elements. if you are listing by mapping an array, you can use the id's of the items you are listing as

const handleDelete = (id) => {
// fetchAPI operations using the "id" in params
}

arrayToUse.map((item) => {

return (
<button onClick={() => {handleDelete(item.id)}}>
DELETE
</button>
)

})

you should render the rows for the items as a complete row to track them better in case you need to reach to API for these kind of things.

i hope this helps, i could help better if you provided some code

  • Related