Action redirect another Action using Partialviewresult

Asked

Viewed 1,032 times

0

public PartialViewResult Index(string quantidadeRegistro)
    {
        int qtd;
        int.TryParse(quantidadeRegistro, out qtd);
        var bdPedido = PedidosAplicacaoConstrutor.PedidosAplicacaoEF();
        var bdCliente = ClientesAplicacaoConstrutor.ClientesAplicacaoEF();
        IEnumerable<Pedidos> pedidos;
        if (qtd < 1)
            pedidos = bdPedido.ListarTodos().OrderByDescending(x => x.ID);
        else
            pedidos = bdPedido.ListarTodos().OrderByDescending(x => x.ID).Take(qtd);
        ViewData["Clientes"] = bdCliente.ListarTodos();
        return PartialView(pedidos);
    }

    [HttpPost]
    public PartialViewResult Editar(FormCollection collection)
    {
        var bdPedido = PedidosAplicacaoConstrutor.PedidosAplicacaoEF();
        var status = collection["status"];
        var id = collection["ID"];

        var pedido = bdPedido.ListarPorId(id);
        pedido.Status = status;
        bdPedido.Salvar(pedido);


        return RedirectToAction("Index");
    }


      function Open(url) {
        $('#Conteudo').empty().append('<div id="loader"><img src="/Content/Images/loading.gif"/></div>');
        $.get(url,function(response){
            $('#Conteudo').html(response);
            $('#loader').remove();
        });
    }

<button type="submit" class="btn btn-success">Salvar</button>

I can’t get Edit to send it to Index. Replaces the Edit Partialviewresult by Actionresult works, but then I lose the "layout" of the page, because I use AJAX.

  • To not lose the layout, Diego, you must use Handler .on. If you post your AJAX, I can help you better.

  • I edited the post, see if this is it!

  • Who calls this their function Open?

  • The Hyperlinks!!

1 answer

1


Asp redirect, will not take effect for an ajax call, what you can do is redirect after the ajax call, with javascript.

Something like that:

function Open(url, redirect) {
    $('#Conteudo').empty().append('<div id="loader"><img src="/Content/Images/loading.gif"/></div>');
    $.ajax({
        type : "POST",
        url : url,
        // aqui você serializa seu form
        data: $("form").find(":input").serialize(),
        success : function(response) {
            if (redirect) {
                $("#Conteudo").load(redirect, function() {
                    $('#loader').remove();
                });
            } else {
                $('#Conteudo').html(response);
                $('#loader').remove();
            }
        }
    });
}

Your call would look like this, for example:

$("form").submit(function(event) {
    Open("/meucontroller/editar", "/meucontroller/index");
});

The method load Jquery will load the page without losing the layout, as I believe you want.

I don’t know if this is viable for you.

  • No intendi @Fernando, this would be on the save button?

  • @Diegozanardo, this is not an ajax call?

  • I have a Partialview with a form that has a Ubmit. This Ubmit sends to "public Partialviewresult Edit(Formcollection Collection){}", this makes an edition in the database, so I need you to return to the Index without losing the layout.

  • @Diegozanardo, I edited my answer, see if now you heed your needs.

  • i need it to be passed by post!

  • @Diegozanardo, but then the problem isn’t even the redirect anymore, so you have to exchange the $.get for the redirect. $.post

  • @Diegozanardo, I edited again from a look.

  • is still a little confused for me. I put my save button in the post. But from what I understand, I’ll have to change the save button to an onclick that points to the open. That? And pass the parameters through the DATA!

  • and how I would treat it in Action?

  • @Diegozanardo, it is not necessary, you can do as posted in the above answer, but can also do on the button.

  • The re-turn of my action would look like?

  • @Diegozanardo, take a look here.

  • I got it here... thanks for the help.

Show 8 more comments

Browser other questions tagged

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