-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))},
– Adjair Costa
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 Santos
Jonas, put your controller code, it’ll be easier to help you.
– Adjair Costa
I don’t think I have privileges to edit my question, but the controller code is on this Dropbox link link
– Jonas Santos
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 apush
on a list and then pass it ondata
of the requisition.– Victor Laio
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!
– Jonas Santos
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
– Victor Laio