Hello!
On this action:
SQL
[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());
}Code
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:
SQL
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")]
}Code
This is the Store class:SQL
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)
- {
- asp-route-id="@item.Id">
-
class="table">
- "hidden" asp-for="@item.Id" />
class="form-group">class="col-md-10">- for="@item.MchName" readonly class="form-control" />
- for="@item.MchName" class="text-danger">
class="form-group">class="col-md-10">- ="@item.StoreID" class="form-control" asp-items="ViewBag.StoreID">
- for="@item.StoreID" class="text-danger">
class="form-group">class="col-md-10">- "number" max="10" step=".1" asp-for="@item.PUnit" class="form-control" />
- for="@item.PUnit" class="text-danger">
class="form-group">class="col-md-10">- asp-for="@item.Status" class="form-control">
- >Operativo
- >Nuevo Item
- >Reparación
- for="@item.Status" class="text-danger">
- "submit" value="Update" class="btn btn-default" />
- }
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());
- }

Nilesh ShahRank #42 · 34.2K reputationPosted Jun 5, 2017, 12:01 PM EDT
Nilesh ShahRank #42 · 34.2K reputationPosted Jun 5, 2017, 7:07 PM EDT
Luis Alberto Delgado de la Flor95 reputationPosted Jun 5, 2017, 4:22 PM EDT
Luis Alberto Delgado de la Flor95 reputationPosted Jun 5, 2017, 7:46 PM EDT