Quantcast
Viewing all articles
Browse latest Browse all 9386

Edit multiple items: HttpPost not working

I'm constructing this view where the user can edit multiple item and save one at the time with the 'Update' button.

Image may be NSFW.
Clik here to view.
)

Problem: I have trouble making the 'Update' button work. Here is the post method:

The idea is to pass the ID of the row that is being edited and save the changes, that's why there is a button for each row. Here is the code for the POST method:

[HttpPost, ActionName("Test")]

public async Task<IActionResult> TestPost(int id)
{

var machinetoUpdate = await _context.Machines
.SingleOrDefaultAsync(s => s.Id == id);
if (await TryUpdateModelAsync(
machinetoUpdate,"",
s => s.MchName, s => s.StoreID, s => s.PUnit, s => s.Status))
{
try
{
await _context.SaveChangesAsync();

}
catch (DbUpdateException)
{
ModelState.AddModelError("", "Unable to save changes. " +"Try again, and if the problem persists, " +"see your system administrator.");
}
return RedirectToAction("Test");
}
return View(await _context.Machines.AsNoTracking().ToListAsync());
}

When I hit the 'Update' button, the table refreshes but with the original data, with no changes recorded. In some point in the POST method this is not working, but I don't receive an explicit error .

This is the code for the view:

@model IEnumerable<Application.Models.Machine>
@{
ViewData["Title"] = "Test";

}

<h2>Management</h2><hr /><table class="table" asp-action="Test"><thead><tr><th>Serial</th><th>Tienda</th><th>Precio por Jugada</th><th>Estado</th><th>Update</th></tr></thead><tbody>
@foreach (var item in Model)
{<tr><td><div class="form-group"><div class="col-md-10"><input asp-for="@item.MchName" readonly class="form-control" /><span asp-validation-for="@item.MchName" class="text-danger"></span></div></div></td><td><div class="form-group"><div class="col-md-10"><select asp-for="@item.StoreID" class="form-control" asp-items="ViewBag.StoreID"><option value="">-- Seleccione Tienda --</option></select><span asp-validation-for="@item.StoreID" class="text-danger"></span></div></div></td><td><div class="form-group"><div class="col-md-10"><input type="number" max="10" step=".1" asp-for="@item.PUnit" class="form-control" /><span asp-validation-for="@item.PUnit" class="text-danger"></span></div></div></td><td><div class="form-group"><div class="col-md-10"><select name="Status" asp-for="@item.Status" class="form-control"><option value="0">Operativo</option><option value="1">Nuevo Item</option><option value="2">Reparación</option></select><span asp-validation-for="@item.Status" class="text-danger"></span></div></div></td><td>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()<input type="hidden" value="@item.Id" name="Id" /><input type="submit" value="Update" />
}</td></tr>
}</tbody></table>

Any help and what I might be doing wrong in the Post Method is welcome. Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>