Post com form + string

Asked

Viewed 41 times

2

I own 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 the post I can’t get the two variables to be filled, so it’s in Function, only the typeMeioPublication is filled and the Purchase is null. I tried to use a serialized object as well, as follows:

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

This way, the Purchase will be filled, but the typeMeioPublication will null.

How to send everything on post?

1 answer

1


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

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

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

var parametro = {
   tipoMeioPublicacao: meioPublicacao
};

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

Then just pass the direct amount by your $.post():

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

$.post(form.attr("action"), parametrosPost , function (data) {
 //seu metodo
});
  • 1

    Perfect. Thank you for the reply. ^^

Browser other questions tagged

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