Zar*_*raQ 1 asp.net asp.net-mvc entity-framework entity-framework-5
这是当前的基本代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Registration registration)
{
if (ModelState.IsValid)
{
db.Entry(registration).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(registration);
}
Run Code Online (Sandbox Code Playgroud)
我在注册表中有大约15个字段,我怎么只想更新"日期"字段,我在这里收到的对象是"注册",它只有日期值,但是当前代码更新所有条目,什么我想要的是更新"日期"字段,我在"注册"中获得了它的价值
帮助将不胜感激:)
将其附加到Unchanged状态中的上下文,并仅设置Date为已修改.
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");
}
Run Code Online (Sandbox Code Playgroud)
你说传入的实体只是Date填写了,希望也有一个Id.:)
| 归档时间: |
|
| 查看次数: |
4551 次 |
| 最近记录: |