Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 149785

How to change variable value using PUT method?

$
0
0

I need to change the value of a variable when a button is clicked. For that I made a function that updates the database using ajax. Here is what I have so far:

 function atualizaBD(novoEstado) {
    $.ajax
        ({
            url:`/api/IgnicoesAPI/${id}`,
            type: 'PUT',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                Id : id,
                Estado: novoEstado
            }),
            success: function (result) {
                alert(result);

            },

            error: function () {
                alert("ocorreu um erro!")
            }
        });
}

I have a variable called Estado and I want to change the value of that variable to a new one, novoEstado. Here is my controller:

     [HttpPut("{id}")]
    public async Task<IActionResult> PutIgnicoes([FromRoute] int id, [FromBody] Ignicoes ignicao, string novoEstado)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }


        if (id != ignicao.Id)
        {
            return BadRequest();
        }


          var ig = _context.Ignicoes.FirstOrDefault (ignicaoId => ignicaoId.Id.Equals(id));
        ig.Estado = novoEstado;

        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!IgnicoesExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return NoContent();
    }

Right now, everytime the function an error occurs alerting "ocorreu um erro"

Here is my Model:

  public class Ignicoes
{

    public Ignicoes()
    {
        ListaOcorrencias = new HashSet<Ocorrencias>();

    }
    [Key]
    public int Id { get; set; }
    public string Latitude { get; set; }

    public string Longitude { get; set; }

    //estado(recusada, aceite, em avaliacao, concluido)
    public string Estado { get; set; }


    public DateTime DataInicioPropostaIgnicao { get; set; }
    public DateTime DataDecisaoIgnicao { get; set; }




    //lista de ocorrencias 
    public virtual ICollection<Ocorrencias> ListaOcorrencias { get; set; }

}

}


Viewing all articles
Browse latest Browse all 149785

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>