3
Answers

ASP.NET Core: Data Grid - Problem with Update Operation

Hello,
 
I'm constructing this view where the user can edit multiple item and save one at the time with the 'Update' button.
 
 
Right now, when I hit the Update button to save the changes,
1.- It goes to this address: http://localhost:60288/1, where '1' is the ID of the Machine inside the Database and the website is blank
2.- It does not update the register with the changes made  :(
 
I've been struggling with this for days, and I hope I can find some help here. 
 
This is the Put Method:
 
  1. [HttpPut("{id}"), ActionName("Test")]  
  2.         [ValidateAntiForgeryToken]  
  3.         public async Task<IActionResult> TestPost(int id, Machine machinetoUpdate)  
  4.         {  
  5.             if (!ModelState.IsValid)  
  6.             {  
  7.                 return BadRequest(ModelState);  
  8.             }  
  9.             if (id != machinetoUpdate.Id)  
  10.             {  
  11.                 return BadRequest();  
  12.             }  
  13.             _context.Entry(machinetoUpdate).State = EntityState.Modified;  
  14.               
  15.             try  
  16.             {  
  17.                 await _context.SaveChangesAsync();  
  18.   
  19.             }  
  20.             catch (DbUpdateException)  
  21.             {  
  22.                 ModelState.AddModelError("""Unable to save changes. " +  
  23.                     "Try again, and if the problem persists, " +  
  24.                     "see your system administrator.");  
  25.             }  
  26.               
  27.               
  28.             //PopulateMachineTypeDropDownListStore();  
  29.               
  30.             return View(await _context.Machines.AsNoTracking().ToListAsync());  
  31.         }  
 And this is the Get Method:
 
  1. public async Task<IActionResult> Test()  
  2.         {  
  3.             PopulateMachineTypeDropDownListStore();  
  4.             return View(await _context.Machines.AsNoTracking().ToListAsync());  
  5.         }  
 And this is the View:
 
  1. @model IEnumerable<Application.Models.Machine>  
  2. @{  
  3.     ViewData["Title"] = "Test";  
  4.   
  5. }  
  6.   
  7. <h2>Management</h2>  
  8. <hr />  
  9.   
  10. <table class="table">  
  11.     <thead>  
  12.         <tr>  
  13.             <th>Serial</th>  
  14.             <th>Tienda</th>  
  15.             <th>Precio por Jugada</th>  
  16.             <th>Estado</th>  
  17.             <th>Update</th>  
  18.         </tr>  
  19.     </thead>  
  20. </table>  
  21.   
  22. @foreach (var item in Model)  
  23. {  
  24.     <form asp-action="Test" asp-route-id="@item.Id">  
  25.         <table class="table">  
  26.             <tbody>  
  27.                 <tr>  
  28.                     <td>  
  29.                         <input type="hidden" asp-for="@item.Id" />  
  30.                         <div class="form-group">  
  31.                             <div class="col-md-10">  
  32.                                 <input asp-for="@item.MchName" readonly class="form-control" />  
  33.                                 <span asp-validation-for="@item.MchName" class="text-danger"></span>  
  34.                             </div>  
  35.                         </div>  
  36.                     </td>  
  37.                     <td>  
  38.                         <div class="form-group">  
  39.                             <div class="col-md-10">  
  40.                                 <select asp-for="@item.StoreID" class="form-control" asp-items="ViewBag.StoreID">  
  41.                                     <option value="">-- Seleccione Tienda --</option>  
  42.                                 </select>  
  43.                                 <span asp-validation-for="@item.StoreID" class="text-danger"></span>  
  44.                             </div>  
  45.                         </div>  
  46.                     </td>  
  47.                     <td>  
  48.                         <div class="form-group">  
  49.                             <div class="col-md-10">  
  50.                                 <input type="number" max="10" step=".1" asp-for="@item.PUnit" class="form-control" />  
  51.                                 <span asp-validation-for="@item.PUnit" class="text-danger"></span>  
  52.                             </div>  
  53.                         </div>  
  54.                     </td>  
  55.                     <td>  
  56.                         <div class="form-group">  
  57.                             <div class="col-md-10">  
  58.                                 <select name="Status" asp-for="@item.Status" class="form-control">  
  59.                                     <option value="0">Operativo</option>  
  60.                                     <option value="1">Nuevo Item</option>  
  61.                                     <option value="2">Reparación</option>  
  62.                                 </select>  
  63.                                 <span asp-validation-for="@item.Status" class="text-danger"></span>  
  64.                             </div>  
  65.                         </div>  
  66.                     </td>  
  67.                     <td>  
  68.                         <input type="submit" value="Update" class="btn btn-default" />  
  69.                     </td>  
  70.                 </tr>  
  71.             </tbody>  
  72.         </table>  
  73.     </form>}  
 Thanks in advance for any help. If any extra information is needed, please ask.
 
Regards,
 
Luis. 
 
 
 
Answers (3)