Post with form + string

2

I have the following function:

var form = $("#formEditarCompra");
var meioPublicacao = $("#MeioPublicacao").val();

    if (form.valid() && setarFocoErro()) {
        mostrarAguarde();
        $.post(form.attr("action"), { compra: form.serialize(), tipoMeioPublicacao: meioPublicacao }, function(data) {
            var result = data;
            if (result.Erro) {
                mostrarAlerta(result.ErroMsg, 3, "erro");
            } else {
                mostrarAlerta(result.SucessoMsg, 5);
                gridCompra.fnDraw();
            }

            esconderAguarde();
        });
    }

And in Controller the following Action:

public ActionResult Editar(Compra compra, string tipoMeioPublicacao);

In%% I can not get the two variables to be filled, so it's in function, only the TypePublication type is filled and the Purchase is null. I tried to use a serialized object too, as follows:

var compraEditar = {
        compra: form.serialize(),
        tipoMeioPublicacao: meioPublicacao
}
$.post(form.attr("action"), JSON.stringify(compraEditar), function (data)

In this way, the Purchase is completed, however the typePublication goes null.

How do I send everything in post ?

    
asked by anonymous 03.11.2015 / 15:16

1 answer

1

When you do a .serialize() it generates a querystring, to add parameters to it you can use one of the following ways:

(form.serialize() + ("&tipoMeioPublicacao=" + meioPublicacao));

Or you can use the $.param() that I find most elegant:

var parametro = {
   tipoMeioPublicacao: meioPublicacao
};

(form.serialize() + '&' + $.param(parametro));

Then just pass the direct value through your $.post() :

var parametrosPost = (form.serialize() + '&' + $.param(parametro));

$.post(form.attr("action"), parametrosPost , function (data) {
 //seu metodo
});
    
03.11.2015 / 15:55