2
Reply

Class sent to Post Action is null

Luis Alberto Delgado de la Flor

Luis Alberto Delgado de la Flor

Jun 6 2017 8:27 PM
226

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: 
  1. public async Task<IActionResult> Test()  
  2.     {  
  3.   
  4.         PopulateMachineTypeDropDownListStore();  
  5.         return View(await _context.Machines.AsNoTracking().ToListAsync());  
  6.     }  
Post Method: 
  1. [HttpPost, ActionName("Test")]  
  2.     [ValidateAntiForgeryToken]  
  3.     public async Task<IActionResult> TestPost(int id, [Bind("Id,StoreID,PUnit,Status")] Machine machine)  
  4.     {  
  5.         if (id != machine.Id)  
  6.         {  
  7.             return NotFound();  
  8.         }  
  9.         if (ModelState.IsValid)  
  10.         {  
  11.             try  
  12.             {  
  13.                 _context.Update(machine);  
  14.                 await _context.SaveChangesAsync();  
  15.             }  
  16.             catch (DbUpdateConcurrencyException)  
  17.             {  
  18.                 if (!MachineExists(machine.Id))  
  19.                 {  
  20.                     return NotFound();  
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     throw;  
  25.                 }  
  26.             }  
  27.             return RedirectToAction("Index");  
  28.         }  
  29.         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:
 
  1. @model IEnumerable<Application.Models.Machine>  
  2. @{  
  3.     ViewData["Title"] = "Test";  
  4.   
  5. }  
  6. @foreach (var item in Model)  
  7. {  
  8.     <form asp-action="Test" asp-route-id="@item.Id" method="post">  
  9.         <table class="table">  
  10.             <tbody>  
  11.             <tr>  
  12.                 <td>  
  13.                     <input type="hidden" asp-for="@item.Id" />  
  14.                     <div class="form-group">  
  15.                         <div class="col-md-10">  
  16.                             <input asp-for="@item.MchName" readonly class="form-control" />  
  17.                             <span asp-validation-for="@item.MchName" class="text-danger"></span>  
  18.                         </div>  
  19.                     </div>  
  20.                 </td>  
  21.                 <td>  
  22.                     <div class="form-group">  
  23.                         <div class="col-md-10">  
  24.                             <select asp-for="@item.StoreID" class="form-control" asp-items="ViewBag.StoreID">  
  25.                             </select>  
  26.                             <span asp-validation-for="@item.StoreID" class="text-danger"></span>  
  27.                         </div>  
  28.                     </div>  
  29.                 </td>  
  30.                 <td>  
  31.                     <div class="form-group">  
  32.                         <div class="col-md-10">  
  33.                             <input type="number" max="10" step=".1" asp-for="@item.PUnit" class="form-control" />  
  34.                             <span asp-validation-for="@item.PUnit" class="text-danger"></span>  
  35.                         </div>  
  36.                     </div>  
  37.                 </td>  
  38.                 <td>  
  39.                     <div class="form-group">  
  40.                         <div class="col-md-10">  
  41.                             <select name="Status" asp-for="@item.Status" class="form-control">  
  42.                                 <option value="0">Operativo</option>  
  43.                                 <option value="1">Nuevo Item</option>  
  44.                                 <option value="2">Reparación</option>  
  45.                             </select>  
  46.                             <span asp-validation-for="@item.Status" class="text-danger"></span>  
  47.                         </div>  
  48.                     </div>  
  49.                 </td>  
  50.                 <td>  
  51.                     <input id="submit-data" type="submit" value="Update" class="btn btn-default" />  
  52.                 </td>  
  53.             </tr>  
  54.         </tbody>  
  55.     </table>  
  56.   
  57. </form>}  


Answers (2)