I cannot load a View after calling $.post

Asked

Viewed 56 times

3

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

The parameter arrives correctly in my action, but I cannot return to View that I desire. Probably, the $.post is waiting for this return, so the View is not loaded.

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

The function jQuery:

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

The Controller:

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

   return View("ListarCliente", cliente);
}

In case, I can’t load the View.

Thank you all.

1 answer

1

Ajax requests do not automatically redirect. And their javascript function is not receiving the return of the action.

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 for:

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 view content 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 continues as it was.

Browser other questions tagged

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