存储更新,插入或删除语句影响了意外数量的行(0)。实体可能已修改或删除
在尝试保存编辑页面时,我发现了以下实体框架错误,
"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded."
这是我的编辑控制器
[HttpPost]
public ActionResult Edit(RESULT results)
{
if (ModelState.IsValid)
{
_context.Entry(results).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Details");
}
return View(results);
}
,我需要更新多个行(仅结果列值)
,我检查了网站中的解决方案
,我添加了主密钥ID列,请参见图像:
这是编辑视图代码:
<table class="table">
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().ID)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().custid)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().TESTID)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().LabTest.TestName)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().RESULT1)
</td>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.TextBox("RESULTS[" + @i + "].ID", Model[i].ID, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].custid", Model[i].custid, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].TESTID", Model[i].TESTID, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("LabTest[" + @i + "].TestName", Model[i].LabTest.TestName, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].RESULT1", Model[i].RESULT1)
</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10" style="margin:100px;">
<input type="submit" value="Save" class="btn btn-danger" />
</div>
</div>
我的代码中缺少什么,请我需要您的帮助?
I got the following entity framework error while trying to save edit page
"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded."
This is my EDIT controller
[HttpPost]
public ActionResult Edit(RESULT results)
{
if (ModelState.IsValid)
{
_context.Entry(results).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Details");
}
return View(results);
}
I need to update multiple rows (Result column value only)
and I checked the solutions in the site
and I added the primary key ID column see the image :
this is the EDIT view code :
<table class="table">
<tr>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().ID)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().custid)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().TESTID)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().LabTest.TestName)
</td>
<td>
@Html.DisplayNameFor(model => model.FirstOrDefault().RESULT1)
</td>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.TextBox("RESULTS[" + @i + "].ID", Model[i].ID, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].custid", Model[i].custid, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].TESTID", Model[i].TESTID, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("LabTest[" + @i + "].TestName", Model[i].LabTest.TestName, new { @readonly = "readonly" })
</td>
<td>
@Html.TextBox("RESULTS[" + @i + "].RESULT1", Model[i].RESULT1)
</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10" style="margin:100px;">
<input type="submit" value="Save" class="btn btn-danger" />
</div>
</div>
What is the missing in my code please I need your help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的控制器操作上,参数应为列表。您需要迭代并更新。
看法
On your Controller Action, the parameter should be the List. You need to iterate and update.
View
我找到了解决方案,我将代码更改为以下:
编辑控制器代码:
编辑视图代码:
I found the solution I changed the code as the following :
Edit controller code :
EDIT view code :
基于@RAM Anugandula逻辑进行了修改,我们可以在循环完成后保存数据,因为对象是参考类型,我们可以保存修改整个对象后
Modified based on @Ram Anugandula Logic we can save data after completion of loop because objects are reference type and we can save the after modify entire object