jquery – I can't load a View after calling $.post

Question:

I'm passing the parameter below (client), from a jQuery function to an Action , through the $.post command (as shown below).

The parameter arrives correctly in my action , but I can't return to the View I want. The $.post is probably waiting for this return, so the View doesn't load.

Does anyone know if there is a way to disable this return to $.post , or any other suggestions so I can load my View ?

The jQuery function:

function CriarCliente() {
    var cliente =
    {
        CLIENTE_NOME: $('#CRIARCLIENTE_NOME').val().trim().toUpperCase(),
        CLIENTE_RG: $('#CRIARCLIENTE_RG').val().trim()
    }

    var urlDestino = "/CadastroPessoa/Cliente/CriarCliente";

    $.post(urlDestino, { cliente: cliente }) 
}

O Controller:

public ActionResult CriarCliente(ClienteEnt cliente)
{
   .........CÓDIGO.....

   return View("ListarCliente", cliente);
}

In this case, I can't load the View .

Thank you all.

Answer:

Ajax requests do not automatically redirect. And your javascript function is not getting the action's return.

If your intention is to redirect the user to another page. You should change your JavaScript function to something like:

function CriarCliente(){
    var cliente =
    {
        CLIENTE_NOME: $('#CRIARCLIENTE_NOME').val().trim().toUpperCase(),
        CLIENTE_RG: $('#CRIARCLIENTE_RG').val().trim()
    }

    var urlDestino = "/CadastroPessoa/Cliente/CriarCliente";

    $.post(urlDestino, { cliente: cliente }, function(data){
        //Se retorno OK
        window.location.replace(data.url);
    }).fail(function(){
        alert("erro.");
    });
}

And your controller stops:

public ActionResult CriarCliente(ClienteEnt cliente)
{
   .........CÓDIGO.....
    if(Request.IsAjaxResquest()) // Se for usa requisição Ajax retorno a url para redirecionamento.
        return Json(new {url = "/Controller/Action/"});

    //Senão. Retorno a View Cliente.
    return View("ListarCliente", cliente);
}

If your intention is just to display the content of the view somewhere on the current page. You should change your JavaScript function to something like:

function CriarCliente(){
    var cliente =
    {
        CLIENTE_NOME: $('#CRIARCLIENTE_NOME').val().trim().toUpperCase(),
        CLIENTE_RG: $('#CRIARCLIENTE_RG').val().trim()
    }

    var urlDestino = "/CadastroPessoa/Cliente/CriarCliente";

    $.post(urlDestino, { cliente: cliente }, function(data){
        //Se retorno OK
        $("#container").html(data);
    }).fail(function(){
        alert("erro.");
    });
}

And your controller remains as it was.

Scroll to Top
AllEscort