4
I own a view
who lists all my customers. Each customer has 3 checkbox
where the user selects them, and sends them to the controller
- through the button (Submit) - the data to change.
The method is working perfectly. However, when sending, it sends all customers, even those that have not changed. Thus it updates all (even those that have not been modified). This does not cause me problems, but causes a delay (since he has to go through the entire list to change).
I would like to know if there is a way to check if the client has been modified, and send only those who have been to my controller
.
My View
is that way:
@using PrestacaoWeb.UI.Helpers
@model List<PrestacaoWeb.Application.ViewModels.ClientePrestacaoViewModel>
@using (Html.BeginForm("EditarMes", "Cliente", FormMethod.Post))
{
<input type="submit" value="Salvar"/>
<table>
<thead>
<tr>
<th> Cliente </th>
<th> 1 </th>
<th> 2 </th>
<th> 3 </th>
<th> Cidade </th>
<th> Responsável </th>
<th> Obs </th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
@Html.HiddenFor(model => model[i].ClienteId)
<td style="width: 100px">
<p align="center">@Model[i].NomeCliente</p>
</td>
<td style="width: 20px"><p align="center"> <b>@Html.CheckBoxFor(model => Model[i].EnvioPrestacao.bJaneiro)</b> </p> </td>
<td style="width: 20px"><p align="center"> <b>@Html.CheckBoxFor(model => Model[i].EnvioPrestacao.bFevereiro)</b> </p> </td>
<td style="width: 20px"><p align="center"> <b>@Html.CheckBoxFor(model => Model[i].EnvioPrestacao.bMarco)</b> </p> </td>
<td>
@Model[i].Cliente.Cidade.Nome
</td>
<td style="width: 100px">
<p align="center">@Model[i].Responsavel</p>
</td>
<td style="width: 210px">
<p align="center">@Model[i].Observacao.</p>
</td>
</tr>
}
</tbody>
</table>
}
By clicking the button Save, it sends all objects from the list to the method Editarmes, in my Controller
.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditarMes(IList<ClientePrestacaoViewModel> prestacaoViewModel)
{
if (!ModelState.IsValid) return View("Index");
foreach (var item in prestacaoViewModel.Where(item => item != null))
{
_clienteContext.Atualizar(item);
TempData["MensagemSuccess"] = "Cliente alterado com sucesso!";
}
return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
}
If I change all clients, you’re right, it will take a while. Now, if I change only one customer, and it sends 30 to controller
, it wouldn’t be "nice" to wait for this.
You could implement a custom Model Binder (Imodelbinder implementation) that would make Binding only the modified elements. This would not make the post smaller but would dispense with foreach within the controller.
– Rafael Companhoni
@rcompanhoni You would have some example of any implementation?
– Wise