这是当前的基本代码:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Registration registration) { if (ModelState.IsValid) { db.Entry(registration).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(registration); }
我在注册表中有大约15个字段,我怎么只想更新“日期”字段,我在这里收到的对象是“注册”,它只有日期值,但是当前代码更新所有条目,什么我想要的是更新“日期”字段,其值已经在“注册”中获得
帮助将不胜感激:)
解决方法@H_404_10@
将其附加到Unchanged状态的上下文中,并仅将Date设置为modified.
if (ModelState.IsValid)
{
db.Registrations.Attach(registration); // attach in the Unchanged state
db.Entry(registration).Property(r => r.Date).IsModified = true;
// Date field is set to Modified (entity is now Modified as well)
db.SaveChanges();
return RedirectToAction("Index");
}
你说传入的实体只有日期填写,希望也有一个Id. 原文链接:https://www.f2er.com/aspnet/251163.html
if (ModelState.IsValid) { db.Registrations.Attach(registration); // attach in the Unchanged state db.Entry(registration).Property(r => r.Date).IsModified = true; // Date field is set to Modified (entity is now Modified as well) db.SaveChanges(); return RedirectToAction("Index"); }
你说传入的实体只有日期填写,希望也有一个Id. 原文链接:https://www.f2er.com/aspnet/251163.html