The key might be assigned, because if its not, then Add will be used, but i don't know how to make it assigned. When i edited the entity instead of editing the new entity was added.
model:
public class Products
{
[Key, NotNull, Required]
public int ProductId { get; set; }
[Required, NotNull]
public string Name { get; set; }
public int Price { set; get; }
public int Quantity { set; get; }
}
controller:
{
if (id == null || id == 0)
{
return NotFound();
}
var productFromDb = _db.Products.Find(id);
if (productFromDb == null)
{
return NotFound();
}
return View(productFromDb);
}
//post
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Products obj)
{
if (ModelState.IsValid)
{
_db.Products.Update(obj);
_db.SaveChanges();
return RedirectToAction("Products");
}
return View(obj);
}
`