CRUD_React
axios way to do Edit/Update (PUT) operations:
updateSupplier() {
var privateId = this.state.supplier.supplierID;
var myData = JSON.stringify({
SupplierID: privateId,
CompanyName: document.querySelector(".companyName").value,
ContactName: document.querySelector(".contactName").value,
ContactTitle: document.querySelector(".contactTitle").value,
Address: document.querySelector(".address").value,
City: document.querySelector(".city").value,
Region: document.querySelector(".region").value,
PostalCode: document.querySelector(".postalCode").value,
Country: document.querySelector(".country").value,
Phone: document.querySelector(".phone").value,
Fax: document.querySelector(".fax").value,
HomePage: document.querySelector(".homePage").value
});
const headers = { 'Content-Type': 'application/json' };
axios.put('API/Suppliers/' + this.state.supplier.supplierID, myData, { headers })
.then((data) => {
if (data.status === 200) {
var t = "Saved , OK";
document.querySelector("#punk").textContent = t;
this.ShowMessage("Updated OK !!");
}
})
.catch((err)=>{
console.log(err);
})
};
Server-side C# code:
[HttpPut("{id}")]
public async Task
PutSuppliers(int id, Suppliers suppliers)
{
if (id != suppliers.SupplierID)
{
return BadRequest();
}
_context.Entry(suppliers).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SuppliersExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return Ok(); //NoContent();
}