What is the correct way to send a form to a controller via AJAX?

Asked

Viewed 78 times

2

I have a simple form, which is basically my Viewmodel. I am doing a post via AJAX passing this form, however, on the server side, everything is always null. My AJAX is as follows:

var formData = $('form').serialize();
$.ajax({
    url: '/configuracaocomercial/Create',
    type: "post",
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    data: { viewModel: formData },
    success: function (data) {
        // meu callback
    }
});

I had read in a question in the OS in English, that the problem could be the contenttype, so I put this 'application/x-www-form-urlencoded; charset=UTF-8'. However, my problem persists. On the server side, I simply get it:

[HttpPost]
public ResultadoAjax Create(ConfiguracaoComercialViewModelEdit viewModel)
{
    // Aqui, a viewModel já vem null
    // meu código
    // ...
    return result;
}
  • Remove viewmodel and place data: formData and testing

  • @Virgilionovic worked, the problem is I have to pass another parameter to function

  • @Arturotemplário until I put as answer, which parameter? that needs more???

2 answers

1

Try to use in the view the following command:

@using (Ajax.BeginForm("ACTION", "CONTROLLER",
new AjaxOptions
{
    HttpMethod = "POST",
    OnBegin = "$.fn.ExibirLoading();",
    OnSuccess = "CarregarServicoTerceiroServicoComplete(data);",
    OnFailure = "$.fn.VerifyError(xhr, status, error);",
    OnComplete = "$.fn.OcultarLoading();$('#FormCadastrarServico')[0].reset();"
}, new { @id = "FormCadastrarServico" }))
                    {
    //SEU FORMULARIO    
}

And . net will automatically call via ajax, I believe this is the best way.

The methods OnBegin, OnSuccess, OnFailure, OnComplete call js to the actions cited.

1

From what I can tell, there’s an item in the way viewModel, I believe you should do this:

Change this

var formData = $('form').serialize();
$.ajax({
    url: '/configuracaocomercial/Create',
    type: "post",
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    data: { viewModel: formData },
    success: function (data) {
        // meu callback
    }
});

therefore:

var formData = $('form').serialize();
$.ajax({
    url: '/configuracaocomercial/Create',
    type: "post",
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    data: formData,
    success: function (data) {
        // meu callback
    }
});
  • Excellent, this way from below he passed the viewModel. But I have one more parameter to pass, so my AJAX was that way. The original signature of the method is: public ResultadoAjax Create(ConfiguracaoComercialViewModel viewModel, List<ProdutoTabela> produtos_tabela)

  • Within the <form> has that list of ProdutoTabela, Because if you have it right, it’s like this??? then it depends on the form! @Arturotemplário

  • The List<Productbeauty> I ride in hand. Funny that if I step one at a time, it works

  • it is because there is a way to pass, the problem is how you are going, understood @Arturotemplário, and one thing this is not in the original question, if it could open a new question for that particular Paramento, what do you think!

Browser other questions tagged

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