I have in my database one to two rows that can and will be updated at a given time based on the Id. This is a List<> of data that I'm able to send to the View and submit it to the [HttpPost] method in the controller just fine but when I call the update method I've created, It finds the corresponding rows and goes thru the foreach loop supposedly updating them but really just adds the updated data as two new rows to the table. I cant seem to figure out what Im doing wrong.
Table before update
+------+--------+-------------+------------+-------------+
| ID | ItemID | PriceTypeID | ListTypeID | DirectPrice |
+------+--------+-------------+------------+-------------+
| 1021 | 62238 | 2 | 1 | 0.00 |
| 1022 | 62238 | 3 | 1 | 0.00 |
+------+--------+-------------+------------+-------------+
Update Function
public static void UpdateItem(List<PricingOptionDTO> pricingOptionDTO)
using (DBContext context = new DBContext()) {
foreach (var item in pricingOptionDTO)
{
Entities.PricingOption pricingOptionTb = context.PricingOptions.Where(a => a.ID.Equals(item.ID)).FirstOrDefault();
if (pricingOptionTb != null)
{
pricingOptionTb .DirectPrice = item.DirectPrice;
pricingOptionTb .Increment = item.BidIncrement;
pricingOptionTb .WarningCap = item.BidWarningCap;
}
context.PricingOptions.Add(pricingOptionTb);
context.SaveChanges();
}
context.SaveChanges();
}
Table after update
+------+--------+-------------+------------+-------------+
| ID | ItemID | PriceTypeID | ListTypeID | DirectPrice |
+------+--------+-------------+------------+-------------+
| 1021 | 62238 | 2 | 1 | 0.00 |
| 1022 | 62238 | 3 | 1 | 0.00 |
| 1023 | 62238 | 2 | 1 | 1.00 |
| 1024 | 62238 | 3 | 1 | 2.00 |
+------+--------+-------------+------------+-------------+