I am attempting to delete a row by using the id on the row. The page will load but in dev tools it is telling me that the delete method is not allowed.
I have tried - adding the the method delete and removing it - adding and removing headers
so far nothing has worked
handleDelete = patientId => {
fetch("https://localhost:5001/api/PtSearchPg/delete" + patientId, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ patientId })
})
};
This is my table with the delete button on it
<tbody>
{this.state.patients.map(patient => (
<tr key={patient.patientId}>
<td>{patient.firstName}</td>
<td>{patient.lastName}</td>
<td>{patient.dob}</td>
<td>{patient.unitId}</td>
<td align="center">
<button
type="button"
className="btn btn-sm btn-warning btnspace"
>
Edit
</button>
<button
id="delete"
type="button"
className="btn btn-sm btn-danger"
onClick={this.handleDelete(patient.patientId)}
>
Delete
</button>
</td>
</tr>
))}
</tbody>
This is the API
// DELETE api/patients/5
[HttpDelete("/delete/{id}")]
public void Delete(int id)
{
using (var DB = new AeffectContext())
{
Patient deletePt = new Patient() { PatientId = id };
DB.Patient.Attach(deletePt);
DB.Patient.Remove(deletePt);
DB.SaveChanges();
}
}
So I should be deleting the patient on the row based on their id, but right now all I am getting is the 405 error.
I am new to coding so I'm sure you'll find other things I'm doing wrong haha.