Model list arrives outdated in controller - Asp.Net mvc

Asked

Viewed 76 times

-1

I have a Viewmodel with two object lists. The first of them is has an editable column, where it is possible to change a numerical value.

After changing the values of the "Listtypes" 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 "List Types" in the 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 Listtypes does not have the values changed in the view.

Can someone help me figure out where I went wrong? Thank you!

  • Hello Jonas, try changing this section and see if it works: date: {screen: @Html.Raw(Json.Encode(Model.List of types))},

  • Hello Ajair, Thank you for your quick suggestion! I made the change you suggested and for that I had to change my controller to: " public Actionresult Confirm Takedown(List<Typoscontainer> screen) " The list comes to the controller but with the values outdated. Is that correct the way I am editing the value in the table ?!

  • Jonas, put your controller code, it’ll be easier to help you.

  • I don’t think I have privileges to edit my question, but the controller code is on this Dropbox link link

  • The values are going to the outdated model because in the assembly of the page code by Razor is being passed model of that moment (with the initial values). To send them to the controller through ajax, I recommend mounting an object that will pull the values of the fields and then yes send it after mounting. You can assemble a code for in java script making a push on a list and then pass it on data of the requisition.

  • Good morning Victor, I researched more about your suggestion and I was in doubt of how to capture the values of my list and mount the array in ajax. I have no experience with web, could you give an example of how to do this? Thank you!

  • Here in this topic they teach how you can create your object array, then pass on the date giving a JSON.stringify(). https://answall.com/questions/225013/como-criar-array-de-objetos-em-javascript

Show 2 more comments

1 answer

0

The values are going pro model outdated because in the assembly of the page code by Razor is being passed on model from that moment (With initial values). To send them to the controller via the ajax, recommend to assemble an object that will pull the values of the fields and then yes send it after the assembly. You can mount a code for javascript by making a push on a list and then pass it on data of the requisition.

Here in this topic they teach how you can create your object array, then next pass on the date giving a JSON.stringify().

How to create object array in javascript?

  • Good morning! Actually I need two lists that are in the model, so I would have to send two arrays. I saw the example of how to mount the array, but I don’t know how to reference my list to take the values and mount the array. Thanks

  • Hello! I managed to solve my problem using this site as reference link. I appreciate all your help!

Browser other questions tagged

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