asp.net – 如何使用EF仅更新单个字段
发布时间:2020-05-23 23:06:05 所属栏目:asp.Net 来源:互联网
导读:这是当前的基本代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Registration registration) { if (ModelState.IsValid) { db.Entry(registration).State =
|
这是当前的基本代码: [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个字段,我怎么只想更新“日期”字段,我在这里收到的对象是“注册”,它只有日期值,但是当前代码更新所有条目,什么我想要的是更新“日期”字段,其值已经在“注册”中获得 帮助将不胜感激:) 解决方法将其附加到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. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 如何传递Request.QueryString到Url.Actio
- 我如何保护我的ASP.Net源代码从我的开发人员
- asp.net – Telerik RadGrid – 如何默认编辑模式?
- asp.net – MVC4 – ContextDependentView – 这是什么意思
- asp.net-mvc-4 – MVC4捆绑GZIP和头文件
- asp.net-mvc – Kendo:处理Ajax数据请求中的错误
- asp.net – 部署后初始加载时出现奇怪的log4net引用错误
- asp.net – Html.RenderAction和Html.Action之间的区别
- asp.net-mvc – 在视图中转义JavaScript字符串文字
- asp.net-mvc-3 – AZURE ACS – Windows Live ID – 如何获
推荐文章
站长推荐
热点阅读
