Pass two sets of values from AJAX to controller

0

I need to pass the values of a view model, and a table, the table is passing normally, when passed alone, the viewmodel does not. Here's how 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 input from the form:

 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;
}

But in the controller, in any way I try, one comes empty, or it does not come with the correct data, how can I proceed?

[HttpPost]
    public IActionResult Novo1([FromBody]List<PedidosProdutosF> valores, NovoViewModel model)
    {
        if (ModelState.IsValid)
        {
    
asked by anonymous 27.08.2018 / 21:52

1 answer

1

It's only you to unify the two objects inside one, Ajax only passes only 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)
    {
    }
}

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

    
27.08.2018 / 21:59