I hope to express myself clearly and be able to get some help.
I'm stuck with updating some fields in my database table from the View. Currently I could make it work but only update the data of the first result.
How should it work? Payment orders are entered into to customers that are paying off a debt. Filter the table to show the payment orders that were entered in the month and I have to update the fields of FechaLiq (datetime)
and Liquidada (bool)
.
Use VS 2017 and asp.net Core 2.0.
I read in several posts that the problem comes from using foreach
but I did not know how to adapt my code to what they say here
I leave parts of the code.
Driver
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LiquidaOpj(int? NroOrden)
{
if (NroOrden == null)
{
return NotFound();
}
var opjToUpdate = await _context.Opj.SingleOrDefaultAsync(o => o.NroOrden == NroOrden);
if (await TryUpdateModelAsync<Opj>(
opjToUpdate,
"",
o => o.NroOrden,
o => o.Liquidada,
o => o.FechaLiq))
{
try
{
await _context.SaveChangesAsync();
return View("Success");
}
catch (DbUpdateException)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Fallo al guardar los cambios. " +
"Intente nuevamente, si el error sigue " +
"comuníquese con el administrador.");
}
return RedirectToAction(nameof(Index));
}
return View(opjToUpdate);
}
View (first the search form)
<div class="col-md-6">
<h4>Busqueda</h4>
<hr />
<form method="get">
<div class="form-group">
<label>Fecha Desde:</label>
<input class="form-control input-sm" name="desde" type="date" />
</div>
<div class="form-group">
<label>Fecha Hasta:</label>
<input class="form-control input-sm" name="hasta" type="date" />
</div>
<input type="submit" asp-action="Index" class="btn btn-info btn-sm" value="Buscar" />
</form>
</div>
(form to show results with the submit)
<form asp-action="LiquidaOpj">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Presentacion)
</th>
<th>
@Html.DisplayNameFor(model => model.NroOrden)
</th>
<th>
@Html.DisplayNameFor(model => model.Caratula)
</th>
<th>
@Html.DisplayNameFor(model => model.DNI)
</th>
<th>
@Html.DisplayNameFor(model => model.Liquidada)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Presentacion)
<input name="FechaLiq" type="hidden" value="@item.Presentacion" />
</td>
<td>
@Html.DisplayFor(modelItem => item.NroOrden)
<input name="NroOrden" type="hidden" value="@item.NroOrden" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Caratula)
<input name="Caratula" type="hidden" value="@item.Caratula" />
</td>
<td>
@Html.DisplayFor(modelItem => item.DNI)
<input name="Dni" type="hidden" value="@item.DNI" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Liquidada)
<input name="Liquidada" type="hidden" value="true" style="display:none" checked="checked">
</td>
<td>
<a asp-action="Details" asp-controller="Opjs" asp-route-id="@item.NroOrden">Detalle</a>
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<input type="submit" class="btn btn-success btn-sm" value="Generar" />
</div>
</form>
As you will see add <input type="hidden"/>
to send the controller the data to update.
Any help will be welcome.
Thank you very much for reading.