I'm working on a view that presents all the records of class (Machine
) where some of their properties could be updated.
For this, the view is working with an IEnumerable. These are the Get and Post methods:
Get Method:
- public async Task<IActionResult> Test()
- {
-
- PopulateMachineTypeDropDownListStore();
- return View(await _context.Machines.AsNoTracking().ToListAsync());
- }
Post Method: - [HttpPost, ActionName("Test")]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> TestPost(int id, [Bind("Id,StoreID,PUnit,Status")] Machine machine)
- {
- if (id != machine.Id)
- {
- return NotFound();
- }
- if (ModelState.IsValid)
- {
- try
- {
- _context.Update(machine);
- await _context.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!MachineExists(machine.Id))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return RedirectToAction("Index");
- }
- return View(machine);}
The problem is that the class sent to the Post Action is null (almost all their fields are zero or null except for the Id)
My intention is to update only a few properties of the register thru this view, but for some reason the information from the form is not getting to the Post Method.
Can anyone tell me what is wrong here?
Thanks in advance!
This is the view:
- @model IEnumerable<Application.Models.Machine>
- @{
- ViewData["Title"] = "Test";
-
- }
- @foreach (var item in Model)
- {
- <form asp-action="Test" asp-route-id="@item.Id" method="post">
- <table class="table">
- <tbody>
- <tr>
- <td>
- <input type="hidden" asp-for="@item.Id" />
- <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">
- </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>
- <input id="submit-data" type="submit" value="Update" class="btn btn-default" />
- </td>
- </tr>
- </tbody>
- </table>
-
- </form>}