Hello!
On this action:
- [HttpPost, ActionName("Test")]
- [ValidateAntiForgeryToken]
- public async Task TestPost(Machine machinetoUpdate)
- {
- var machine = _context.Machines.Where(m => m.Id == machinetoUpdate.Id).FirstOrDefault();
-
- machine.PUnit = machinetoUpdate.PUnit;
- machine.StoreID = machinetoUpdate.StoreID;
- machine.Status = machinetoUpdate.Status;
- _context.SaveChanges();
- PopulateMachineTypeDropDownListStore();
-
- return View(await _context.Machines.AsNoTracking().ToListAsync());
- }
I'm getting an error while trying to update StoreID.
SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Machine_Store_StoreID". The conflict occurred in database "Application4", table "dbo.Store", column 'StoreID'. The statement has been terminated.
StoreID is a foreign key for the Machine class:
- public class Machine
- {
- public int Id { get; set; }
- public int TypeID { get; set; }
- public int SupplierID { get; set; }
- public int StoreID { get; set; }
- [StringLength(60)]
- [Display(Name = "Serial")]
- public string MchName { get; set; }
- [DataType(DataType.Date)]
- [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
- [Display(Name = "Fecha de Compra")]
- }
This is the Store class:
- public class Store
- {
- [Key]
- public int StoreID { get; set; }
- public int DistrictID { get; set; }
- [Required]
- [StringLength(60)]
- public string StoreName { get; set; }
- [StringLength(60)]
- public string StoreAddress { get; set; }
-
- public virtual ICollection Machines { get; set; }
- public virtual District Districts { get; set; }
- }
I don't know why StoreID won't update. I'm not inserting a StoreID that does not exist in the Store model, since I choose it from a dropdownlist.
This is the view:
- @foreach (var item in Model)
- {
- <form asp-action="Test" asp-route-id="@item.Id">
- <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 type="submit" value="Update" class="btn btn-default" />
- </td>
- </tr>
- </tbody>
- </table>
-
- </form>}
I don't know if the dropdownlist for the Store might be sending a StoreID value wrong. It seems it's ok, so I don't know.
Thanks in advance for any help.
Update: More Info
I believe my problem could be on my Get Method.
My view (Test.cshtml) is correctly loading and showing the info of all the items in the Machine class in the table, including the StoreID, however, my Get Method is this:
- public async Task Test()
- {
- PopulateMachineTypeDropDownListStore();
- return View(await _context.Machines.AsNoTracking().ToListAsync());
-
- }
Is it possible that it does not matter if my View is showing what I want because my Get Method is not capturing any info on the StoreID? And thus, when I hit the 'Update' button it might be trying to duplicate the StoreID and for that I get the error?