Action redirect another Action using PartialViewResult

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 not get Edit to push Index. Replace the PartialViewResult of the Edit with ActionResult works, but then I lose the layout of the page, because I use AJAX.

    
asked by anonymous 13.06.2014 / 16:08

1 answer

1

The asp redirect will have no effect for a call in ajax, what you can do is to do the redirect after the ajax call with javascript.

Something like this:

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 load method of JQuery will load the page without losing the layout, as I believe you want.

I do not know if this is workable for you.

    
13.06.2014 / 16:21