Updating DbSet<T> item's value from the controller - C#, MVC, Code First

倾然丶 夕夏残阳落幕 提交于 2019-12-08 12:14:26

You are perhaps updating the value in a different DbContext. Try this:

    [HttpPost]
    public ActionResult Edit(Item item)
    {
        if (ModelState.IsValid)
        {
            db.Entry(item).State = EntityState.Modified;
            Qr qr = fqr.FindQr(item.QR, db);
            // update somewhere here
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        ViewBag.SetID = new SelectList(db.Sets, "SetID", "Name", item.SetID);
        return View(item);
    }


public class FindQrs
{
     public Qr FindQr(string qr, SESOContext db)  // pass in the db context
     {
          List<Qr> qrList = db.Qrs.ToList<Qr>();
          Qr foundQr = qrList.Find(delegate(Qr qrDel) { return qrDel.Value == qr; });
          return foundQr;
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!