jsonresult redirects me to page with only json

0

I have a question, I have this method in a view on asp.net MVC 5:

    $('#enviar-data').click(function () {

        //var formData = new FormData($('form')[0]);

        var form = $('#formx');

       // console.log(formData);

        $.ajax({
            type: 'POST',
            // contentType: 'multipart/form-data',
            url: form.prop('action'),
            data: { montoAdicionalOInicial: montoaddic },//form.serialize(),
            //headers: { 'content-type': 'application/json' },
            success: function (data) {
                alert(data.Message);               
            },
            dataType: 'json',
            contentType: false,
            processData: false,
            error: function () { alert("Ocurrio un error inesperado. Reinicie la operacion."); },
            cache: false
        });
    });

This is my controller method:

[HttpPost]
public JsonResult AbrirCajachica(decimal? montoAdicionalOInicial)
{
    MessageModel message = new MessageModel();
    CajaChicaDA objCajaDA = new CajaChicaDA();
    bool existeCajaChicaAbierta = objCajaDA.VerificarCajaChicaAbiertas();
    CajaChica cajadeldiaexiste = objCajaDA.ObtenerCajaChicaDelDia();

    if (!existeCajaChicaAbierta && cajadeldiaexiste == null)
    {
        CajaChica objCajaAnterior = objCajaDA.ObtenerCajaChicaDiaAnterior();

        CajaChica objnuevocaja = new CajaChica();
        objnuevocaja.Fecha = DateTime.Now.Date;
        objnuevocaja.Estado = "A";

        if (objCajaAnterior != null)
        {

            if (montoAdicionalOInicial.HasValue)
            {
                objnuevocaja.MontoInicial = objCajaAnterior.MontoFinal.Value + montoAdicionalOInicial.Value;
            }
            else
            {
                objnuevocaja.MontoInicial = objCajaAnterior.MontoFinal.Value;
            }
        }
        else
        {
            objnuevocaja.MontoInicial = montoAdicionalOInicial.Value;
        }
        int IdCajaChica = objCajaDA.NuevaCajaChica(objnuevocaja);
        int resultstock = 0;
        if (IdCajaChica > 0)
        {
            StockDiarioDA objStockDiarioDA = new StockDiarioDA();
            resultstock = objStockDiarioDA.LLenarStockDiario(IdCajaChica);
        }
        if (resultstock > 0)
        {
            message.Message = "Se creo la caja chica del dia de hoy correctamente;";
            message.Tipo = 1;
        }
        else
        {
            message.Message = "Ocurrio un error.";
            message.Tipo = 2;
        }
    }
    else
    {
        message.Message = "No se puede crear caja chica.";
        message.Tipo = 2;
    }
    return Json(message);
}

So far everything goes well with the data and the process, but when it reaches the success it redirects me to an empty web with the data of the answer json:

{"Message":"No se puede crear caja chica.","Tipo":2}

Is there something I have to configure?

    
asked by cristian gonzalez 20.03.2017 в 00:44
source

1 answer

0

Modify your Javascript:

$('#enviar-data').click(function (e) { //Añade la "e" 
    e.preventDefault();
   …
});

What happens is that the behavior of the button is to go to where it says the action of form , you need to tell it not to do it through preventDefault .

    
answered by 20.03.2017 в 23:13