【发布时间】:2018-08-01 22:11:33
【问题描述】:
当用户发送 GET 请求以编辑记录时,我正在使用 .Include() 加载发送给视图的模型以及相关实体,如下所示:
var client = await _context.Client
.Include(c => c.Office)
.Where(c => c.client_id == id)
.AsNoTracking()
.SingleOrDefaultAsync();
return View(client);
当用户 POST 回编辑表单并且缺少必填字段ModelState.IsValid == false 时,不会执行更新,并且未保存更改的模型将被发送回视图。
public async Task<IActionResult> Edit(Client client_edited )
{
if (!ModelState.IsValid)
{
return View(client_edited); // .Include(c => c.Office) is missing
}
}
如何返回发布的视图模型(用户等待更改)并重新附加所需的 .Include()?
当我重新查询模型(客户端)记录时,挂起的更改会丢失。
我正在尝试做一些事情,比如重新查询带有包含的客户端,然后在顶部复制带有挂起更改的客户端。比如……
var client = await _context.Client
.Include(c => c.Office)
.Where(c => c.client_id == id)
.AsNoTracking()
.SingleOrDefaultAsync();
// need to copy pending client_edited changes to the client
// but does not work because it overwrites the .Include(c => c.Office)
client = client_edited
return View(client_edited);
【问题讨论】:
标签: c# asp.net-core entity-framework-core