Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
JS AJAX removal fails

This code is executing correctly, but the problem is that I cannot get through to the controller so that it performs the deletion JS- Runs without errors and no errors are observed in the Developer Tool. But the controller itself is not executed.

JS

jQueryAjaxDelete = form => {
if (confirm('Are you sure to delete this record ?')) {
    try {
        $.ajax({
            type: 'POST',
            url: form.action,
            data: new FormData(form),
            contentType: false,
            processData: false,
            success: function (res) {
                /*$('#view-all').html(res);*/
                setTimeout(function () { location.reload(); }, 1000);
            },
            error: function (err) {
                console.log(err)
            }
        })
    } catch (ex) {
        console.log(ex)
    }
}

//prevent default form submit event
return false;

}

Controller

       [HttpPost]
    public async Task<ActionResult> CategoryDelete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var category = await _context.Categories.FindAsync(id);
        _context.Categories.Remove(category);
        await _context.SaveChangesAsync();
        return Json(_context.Categories.ToList());

    }

View

              <td><form asp-action="CategoryDelete" asp-route-id="@c.Id" onsubmit="return jQueryAjaxDelete(this)" class="d-inline">
            <input type="submit" value="Delete" class="btn btn-danger" />
          </form></td>

Developer tool

[SOLVED] You need to add routing through attributes [HttpPost], [HttpGet]

        [HttpGet("Admin/CategryDelete/{id}")]//here
    public ActionResult CategoryDelete()
    {
        return View();
    }
    [HttpPost("Admin/CategryDelete/{id}")]//here
    public async Task<ActionResult> CategoryDelete(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var category = await _context.Categories.FindAsync(id);
        _context.Categories.Remove(category);
        _context.SaveChanges();
        return Json(_context.Categories.ToList());

    }
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda