I am currently working on an interface that involves buttons for updating a database. For example, I have a variable named "Estado" which is initially set as "emAvaliacao". When the button "Aceite" is clicked, the value of "Estado" changes to "Aceite".
The following functions are used in this process:
function updateDatabase(markerId, newState) {
$.ajax
({
url: `/api/IgnicoesAPI/${markerId}`,
type: 'PUT',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
Id: +markerId,
Estado: newState
}),
success: function (result) {
connection.invoke("PostMarker").catch(function (err) {
return console.error(err.toString());
});
},
error: function () {
alert("An error occurred!");
}
});
}
Below is the PUT function:
[HttpPut("{id}")]
public async Task<IActionResult> UpdateIgnicoes([FromRoute] int id, [FromBody] Ignicoes ignicao)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != ignicao.Id)
{
return BadRequest();
}
var decisionDate = DateTime.Now;
var ig = _context.Ignicoes.FirstOrDefault(ignicaoId => ignicaoId.Id.Equals(id));
if (ig != null)
{
ig.Estado = ignicao.Estado;
ig.DataDecisaoIgnicao = decisionDate;
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!IgnicoesExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
Currently, I am facing a 400 error and need assistance in resolving it. Can anyone provide guidance on how to tackle this issue?