I am attempting to update a database property named "Estado" using ajax. Here is the code I am using:
function updateDatabaseProperty(idMarker, newState) {
$.ajax
({
url: `/api/IgnicoesAPI/${idMarker}`,
type: 'PUT',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({ Id: idMarker, Estado: newState }),
async: true,
processData: false,
cache: false,
success: function (result) {
connection.invoke("PostMarker").catch(function (err) {
return console.error(err.toString());
});
},
error: function () {
alert("An error occurred!")
}
});
}
Below is my model:
public class Ignicoes
{
public Ignicoes()
{
ListaOcorrencias = new HashSet<Ocorrencias>();
}
[Key]
public int Id { get; set; }
[Required]
public string Latitude {get ;set;}
[Required]
public string Longitude {get; set;}
//estado(recusada, aceite, em avaliacao, concluido)
//public string Estado { get; set; }
[Required]
public string Estado {get;set;}
public DateTime DataInicioPropostaIgnicao {get;set;}
public DateTime DataDecisaoIgnicao{get;set;}
public virtual ICollection<Ocorrencias> ListaOcorrencias {get;set;}
}
Below is my PUT method:
public async Task<IActionResult> UpdateIgnicoes([FromRoute] int id, [FromBody] Ignicoes ignites)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != ignite.Id)
{
return BadRequest();
}
else
{
var decisionDate = DateTime.Now;
var ig = _context.Ignicoes.FirstOrDefault(igniteId => igniteId.Id.Equals(id));
if (ig != null)
{
ig.Estado = ignites.Estado;
ig.DataDecisionIgnicion = decisionDate;
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!IgnicoesExists(id))
{
return NotFound();
}
else
{
throw;
}
}
}
return NoContent();
}
I have tried the above code and noticed that instead of changing the "Estado" property, it changed the "Latitude" property successfully. Both properties are of the same type - String. Can someone help me identify the error? Here is the content from my output tab:
https://i.sstatic.net/w4LOV.png
Here is the Network Analysis: https://i.sstatic.net/sfcKo.png
{"errors":{"Latitude":["The Latitude field is required."],"Longitude":["The Longitude field is required."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|43114da6-4594b09a6260f1a2."}