What I'm looking for is updating a database record by recommending a more experienced programmer who told me that he would use POST to structure the following code.
<script>
$("#txtHoraSalida").val(hs);
$("#txtGafete").keypress(function (e) {
if (e.keyCode === 13) {
var gafete = $("#txtGafete").val();
var datos = {};
datos = {
hora_Salida: $("#txtHoraSalida").val()
}
$.ajax({
url: "http://localhost:49851/api/visita/" + gafete,
method: "POST",
contenttype: "application/json",
data: JSON.stringify(datos),
success: function (data) {
if (data == null) {
ocultarPopUp();
} else {
ocultarPopUpRegistroCompleto();
}
}
});
}
});
</script>
I tried it with PUT but it generates an error 400, with this code compiled the modals and everything but it does not keep the update of the registry I show the code that I have in the controller that calls the update function.
[HttpPost, Route("{salida}")]
public IHttpActionResult ObtenerSalida([FromUri]string gafeteId)
{
try
{
if (string.IsNullOrWhiteSpace(gafeteId) == true)
return BadRequest();
string mensaje = string.Empty;
_visitaService.ActualizarVisita(_visitaService.ObtenerSalida(gafeteId), out mensaje);
return Ok(mensaje);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
and also the function that I have in my service where the update function is called.
public Visitas ObtenerSalida(string gafeteId)
{
try
{
using (var context = new EntradaElectronicaAlmacenEntities())
{
context.Configuration.LazyLoadingEnabled = false;
var visita = context.Visitas.FirstOrDefault(x => x.Gafete == gafeteId && x.Hora_Salida == null);
return visita;
}
}
catch (Exception ex)
{
throw;
}
}
I was missing my update code visit that I have in my service eh tested with postman and everything goes well with my service code but maybe something is missing to work well with ajax
public void ActualizarVisita(Visitas visita, out string mensaje)
{
try
{
using (var context = new EntradaElectronicaAlmacenEntities())
{
context.SaveChanges();
mensaje = "Visita Actualizada de manera exitosa";
}
}
catch (Exception)
{
throw;
}
}
I have modified the code trying to find what is missing so that I can update the registry but I can not find it. Maybe you who have more experience can help me find the missing code portion or correct the one I have in advance thanks ...