Pass two sets of AJAX values to controller

Asked

Viewed 147 times

0

I need to pass the values of a view model, and of a table, the table is passing normally, when passed alone, the viewmodel does not. Go on like I’m doing:

var model1 = objectifyForm(model);
    console.log(model1);
    debugger;
    $.ajax({
        url: '@Url.Action("Novo1", "PedidoFornecedor")',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        data: { valores: JSON.stringify(valores), model: model1 }
    });

This is the function that takes the form input:

 function objectifyForm(formArray) {//serialize data function

    var returnArray = {};
    for (var i = 0; i < formArray.length; i++) {
        returnArray[formArray[i]['name']] = formArray[i]['value'];
    }
    return returnArray;
}

However in the controller, all the way I try, an empty comes, or does not come with the correct data, how can I proceed?

[HttpPost]
    public IActionResult Novo1([FromBody]List<PedidosProdutosF> valores, NovoViewModel model)
    {
        if (ModelState.IsValid)
        {
  • 1

    Mariana you will have to join the two objects in one and send a single object

  • As well, unite the objects ?

  • I’ll write an answer

  • give a look, if it doesn’t work me give details I fix, mainly on the part of creating the new object in c#

  • can put the Novoviewmodel class and what values are passing in the model1 .

1 answer

1


Just by unifying the two objects within one, Ajax only passes one object to the Controller...

var model1 = objectifyForm(model);
var _objetoUnificado = {Model:model1 , Valores:valores}; //passar esse objeto no AJAX
console.log(model1);
debugger;
$.ajax({
    url: '@Url.Action("Novo1", "PedidoFornecedor")',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: { objetoUnificado:_objetoUnificado }
}); 


[HttpPost]
public IActionResult Novo1([FromBody]ObjetoUnificado objetoUnificado)
{
    if (ModelState.IsValid)
    {
    }
}

Ai in your C#, you create an object called ObjetoUnificado with the NovoViewModel and List<PedidosProdutosF>

  • I create the class, with the data, this way: public class ObjetoUnificado {&#xA; NovoViewModel model = new NovoViewModel();&#xA; List<PedidosProdutosF> valores = new List<PedidosProdutosF>();&#xA; }

  • Yes, but put the names equal to what is in javascript, but there is according to what you think best, in my example was "Model" and "Values" uppercase

  • Remember, javascript has to be a mirror of the C object, it’s case sensitive too ;)

  • I tried, changing to the first letter to uppercase, but still, it comes in null. public class ObjetoUnificado {&#xA; NovoViewModel Model = new NovoViewModel();&#xA; List<PedidosProdutosF> Valores = new List<PedidosProdutosF>();&#xA; }

  • How are you doing in ajax? like I did?

  • Similarly, similarly, I tried even passing the parameters as JSON.stringify, and even then, it will always null.

  • Unfortunately, every way I try, it always returns null.

  • @marianac_costa saw in his other question, that the objetoUnificado is minuscule in JS and capitalized in c#

  • He recognizes the class, but does not pass the values, in the Debugger I see the values correctly.

Show 5 more comments

Browser other questions tagged

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