tags:

views:

118

answers:

2

Hi, I have a MVC application with Jquery; I have a gridview with some data and an element which points to a new action of the controller. Now I need to do some validation before executing the action. My code is like this:

$('#datosCartolaSeguro a').click(function(e) {

    var datosProductoSeleccionado = $(this).parent().find('input').val();
    $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
                {
                    strDatosProducto: datosProductoSeleccionado
                },
                function(resp) {
                    if (resp == 'False') {
                        popupActual = '#popupProdNoTieneCartola';
                        centrarPopup();
                        cargarPopup();

                    }
                    else {
                        mostrarVentanaAdvertencia();
                        return true;
                    }
                }
            );
});

After the call to the method: cargarPopup() I want to avoid the execution of the click of the action link. I tried with a simple return false, but the the page does a postback. I also tried with the function e.preventDefault(); that was posted in other solution on this forum, but it doesnt works. I need to avoid the clicking of the ActionLink but with no postback. Thanks.

+3  A: 

Add return false at the end of the click function:

$('#datosCartolaSeguro a').click(function(e) {

    var datosProductoSeleccionado = $(this).parent().find('input').val();
    $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
                {
                    strDatosProducto: datosProductoSeleccionado
                },
                function(resp) {
                    if (resp == 'False') {
                        popupActual = '#popupProdNoTieneCartola';
                        centrarPopup();
                        cargarPopup();

                    }
                    else {
                        mostrarVentanaAdvertencia();
                        return true;
                    }
                }
            );
return false;
});
ryanulit
+1  A: 

You need to stop the even from happening while you are waiting for the response to the post so you should be adding the return false; or e.preventDefault() after the post call.

$('#datosCartolaSeguro a').click(function(e) 
{

 var datosProductoSeleccionado = $(this).parent().find('input').val();
 $.post(pathSite + 'PorSegurosCartola/ProductoTieneCartola',
            {
                strDatosProducto: datosProductoSeleccionado
            },
            function(resp) {
                if (resp == 'False') {
                    popupActual = '#popupProdNoTieneCartola';
                    centrarPopup();
                    cargarPopup();

                }
                else {
                    mostrarVentanaAdvertencia();
                    window.location=$('#datosCartolaSeguro a').href;
                }
            }
        );
e.preventDefault();
};
Jeff Beck
The return false doesn't solve my issue, now the event click never triggers (i mean, the action link never redirects to the page its supposed to) How can I handle it? Thanxs
lidermin
I updated the answer.
Jeff Beck