Call action after a notification

Asked

Viewed 31 times

1

On my index I have this

@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>
}

That will show all data from my regional table.

In the delete link action, I have a function inside the onclick, which passes by parameter the id of the region that was clicked.

When performing this function, an alert is displayed if the person really wants to delete the 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;
            }
        });
}

If the person click yes, the regional has q be excluded.

It is using the Regional controller and the action is Delete.

I’m doubtful how to get to the Delete action with the regional id.

When I click the delete button, the alert appears and deletes kind of the same time the 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 answer

2


You can call the direct url by passing the ID to be deleted in the 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;
            }
        });
}

Or use a request to do so:

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

    thanks Victor, it worked here, obg

Browser other questions tagged

You are not signed in. Login or sign up in order to post.