Model List arrives outdated in controller - Asp.Net mvc

-1

I have a ViewModel with two lists of objects. The first one is an editable column, where you can change a numeric value.

After changing the values of the "ListTipos" in the view, it does not arrive with the new values in the controller.

    public class ViewModelRetirada
    {
        ...
        public IEnumerable<Tipos> ListaTipos { get; set; }
        public IEnumerable<Terminais> ListaTerminais { get; set; }
    }

The "ListTypes" List in View

@foreach (var item in Model.ListaTipos) {
<tr class="table-active" align="center">
  <th scope="row">@item.IdIso</th>
  <td>@item.Reservados</td>
  <td>@item.QtdeRetirada</td>
  <td>@Html.EditorFor(modelItem => item.QtdeDisponivel, new { htmlAttributes = new { @class = "form-control form-control-sm", @type="number"} })</td>
</tr>
}

I am sending the Model as follows:

            $("#ExibeTerminais").click(function () {
                $.ajax({
                    url: '/Retirada/ConfirmarRetirada',
                    type: "post",  
                    data: {tela: @Html.Raw(Json.Encode(Model))},                    
                    success: function (result) {                           
                        $("#DivListarTerminais").html(result)                        
                    },
                    error: function (result) {
                        alert("Ocorreu um erro ao tentar recuperar a lista de terminais! \n Tente mais tarde")
                    }
                })
            })           

When the Model arrives in the Controller, the ListTipos is not with the values changed in the view.

Can anyone help me figure out where I went wrong? Thanks!

    
asked by anonymous 23.10.2018 / 01:53

1 answer

0

The values are going to the% of outdated% because in the assembly of the page code by model is being passed Razor from that moment (with the initial values). To send them to the controller via ajax , I recommend mounting an object that will pull the field values and then send it after assembly. You can mount a for code in javascript by making a model in a list and then passing it on push of the request.

Here, in this topic, you will learn how you can create your array of objects, then go on the date with a JSON.stringify ().

  

How to create array of objects in javascript?

    
24.10.2018 / 12:35