1

Na minha index eu tenho isso

@foreach (var item in Model)
{
    @Html.HiddenFor(modelItem => item.id_regional)

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ds_regional)
        </td>
        <td>
            @Html.ActionLink(" ", "Edit", new { id = item.id_regional }, new { @class = "btn btn-default btn-editar" })
        </td>
        <td>
            @Html.ActionLink(" ", "Delete", new { id = item.id_regional }, new { @class = "btn btn-default btn-apagar", onclick ="AlertaDelete("+item.id_regional+")"})
        </td>
    </tr>
}

Que vai mostrar todos os dados da minha tabela de regional.

Na action link de deletar, eu tenho uma função dentro do onclick, que passa por parâmetro o id da regional que foi clicada.

Ao executar essa função, é exibido um alerta se a pessoa realmente deseja excluir a regional

function AlertaDelete(idReg) {

    $.Zebra_Dialog('<strong>Alerta: </strong> Deseja realmente excluir esta regional?',
        {
            'custom_class': 'tituloZebra',
            'type': 'alert',
            'title': 'CAPACITY',
            'buttons': [
                { caption: 'Cancelar' },
                { caption: 'Sim' },
            ],
            'onClose': function (caption) {
                if (caption == 'Sim') {
                }
                else return false;
            }
        });
}

Caso a pessoa clique em sim, a regional tem q ser excluida.

Ela esta usando o controller Regionais e a action é Delete.

Estou com duvida como fazer chegar na action de Delete com o id da regional.

Quando eu clico no botão delete, aparece o alerta e exclui meio que na mesma hora a regional.

Action Delete

[PermissaoAcao(Permissao = "UsuariosListar", Acao = PermissaoAcao.Acoes.Deletar)]
    public ActionResult Delete(int id)
    {
        using (CapacityBDEntities db = new CapacityBDEntities())
        {
            using (var dbContextTransaction = db.Database.BeginTransaction())
            {
                try
                {
                    cpt_regionais regional = db.cpt_regionais.Find(id);

                    db.cpt_regionais.Remove(regional);
                    db.SaveChanges();
                    dbContextTransaction.Commit();

                    return RedirectToAction("Index", "Regionais");
                }
                catch (Exception e)
                {
                    return RedirectToAction("Index", "Regionais");
                }
            }
        }
    }

1 Resposta 1

2

Você pode chamar a url direto passando o ID a ser deletado no link:

function AlertaDelete(idReg) {

    $.Zebra_Dialog('<strong>Alerta: </strong> Deseja realmente excluir esta regional?',
        {
            'custom_class': 'tituloZebra',
            'type': 'alert',
            'title': 'CAPACITY',
            'buttons': [
                { caption: 'Cancelar' },
                { caption: 'Sim' },
            ],
            'onClose': function (caption) {
                if (caption == 'Sim') {
                    window.location.href = 'SUA_URL_BASE_/SUA_CONTROLLER/Delete/' + idReg;
                }
                else return false;
            }
        });
}

Ou utilizar uma requisição para isso:

function AlertaDelete(idReg) {

    $.Zebra_Dialog('<strong>Alerta: </strong> Deseja realmente excluir esta regional?',
        {
            'custom_class': 'tituloZebra',
            'type': 'alert',
            'title': 'CAPACITY',
            'buttons': [
                { caption: 'Cancelar' },
                { caption: 'Sim' },
            ],
            'onClose': function (caption) {
                if (caption == 'Sim') {
                  $.post('SUA_CONTROLLER_URL/Delete', {id: idReg}, function(data){
                    window.location.href = 'SUA_URL_DE_RETORNO';
                  });
                }
                else return false;
            }
        });
}

1
  • 1
    valeu Victor, funcionou aqui, obg 14/08/2019 às 12:46

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .