存储更新,插入或删除语句影响了意外数量的行(0)。实体可能已修改或删除

发布于 2025-02-12 07:08:48 字数 2777 浏览 0 评论 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 :

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2025-02-19 07:08:48

在您的控制器操作上,参数应为列表。您需要迭代并更新。

    [HttpPost]
    public ActionResult Edit(List<RESULT> results)
    {
       if (ModelState.IsValid)
        {
            foreach(var item in results)
            {
                var model =_context.Find(item.ID);
                if(model != null) {  
                   model.TestID = item.TestID;
                   model.TestName = item.TestName;
                   //update columns you want to update
                   _context.SaveChanges();
                }
            }
            ViewBag.Message = "Updated Successfully";
            return RedirectToAction("Details");
        }
        return View(results);
    }

看法

@if(ViewBag.Message != null){
   @ViewBag.Message
}

On your Controller Action, the parameter should be the List. You need to iterate and update.

    [HttpPost]
    public ActionResult Edit(List<RESULT> results)
    {
       if (ModelState.IsValid)
        {
            foreach(var item in results)
            {
                var model =_context.Find(item.ID);
                if(model != null) {  
                   model.TestID = item.TestID;
                   model.TestName = item.TestName;
                   //update columns you want to update
                   _context.SaveChanges();
                }
            }
            ViewBag.Message = "Updated Successfully";
            return RedirectToAction("Details");
        }
        return View(results);
    }

View

@if(ViewBag.Message != null){
   @ViewBag.Message
}
吻泪 2025-02-19 07:08:48

我找到了解决方案,我将代码更改为以下:

编辑控制器代码:

 public ActionResult Edit(List<RESULT> list)
        {
           if (ModelState.IsValid)
            {
                using (db_Entities db = new db_Entities())
                {
                    foreach (var i in list)
                    {
                        var c = db.RESULTS.Where(a => a.ID.Equals(i.ID)).FirstOrDefault();
                        if (c != null)
                        {
                            c.RESULT1 = i.RESULT1;
                        }
                    }
                    db.SaveChanges();
                    
                }
             //   ViewBag.Message("Updated Successfully");
                return View(list);
            }
            else
            {
              //  ViewBag.Message("Failed ! Please try again");
                return View(list);
            }        

        }

编辑视图代码:

<table class="table">
    <tr>
        
        <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().APPROVED_BY) </td>
        <td> @Html.DisplayNameFor(model => model.FirstOrDefault().APPROVED_DATE) </td>
        <td> @Html.DisplayNameFor(model => model.FirstOrDefault().RESULT1) </td>
       
    </tr>
      @for (int i = 0; i < Model.Count; i++)
      {
          <tr>
           
              <td>@Html.HiddenFor(model => model[i].ID)</td>
              <td>@Html.EditorFor(model => model[i].custid)</td>
              <td>@Html.EditorFor(model => model[i].TESTID)</td>
              <td>@Html.EditorFor(model => model[i].LabTest.TestName)</td>
              <td>@Html.EditorFor(model => model[i].APPROVED_BY)</td>
              <td>@Html.EditorFor(model => model[i].APPROVED_DATE)</td>
              <td>@Html.EditorFor(model => 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>
    @*<p style="color:green;font-size:16px;">
        @ViewBag.Message
    </p>*@

I found the solution I changed the code as the following :

Edit controller code :

 public ActionResult Edit(List<RESULT> list)
        {
           if (ModelState.IsValid)
            {
                using (db_Entities db = new db_Entities())
                {
                    foreach (var i in list)
                    {
                        var c = db.RESULTS.Where(a => a.ID.Equals(i.ID)).FirstOrDefault();
                        if (c != null)
                        {
                            c.RESULT1 = i.RESULT1;
                        }
                    }
                    db.SaveChanges();
                    
                }
             //   ViewBag.Message("Updated Successfully");
                return View(list);
            }
            else
            {
              //  ViewBag.Message("Failed ! Please try again");
                return View(list);
            }        

        }

EDIT view code :

<table class="table">
    <tr>
        
        <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().APPROVED_BY) </td>
        <td> @Html.DisplayNameFor(model => model.FirstOrDefault().APPROVED_DATE) </td>
        <td> @Html.DisplayNameFor(model => model.FirstOrDefault().RESULT1) </td>
       
    </tr>
      @for (int i = 0; i < Model.Count; i++)
      {
          <tr>
           
              <td>@Html.HiddenFor(model => model[i].ID)</td>
              <td>@Html.EditorFor(model => model[i].custid)</td>
              <td>@Html.EditorFor(model => model[i].TESTID)</td>
              <td>@Html.EditorFor(model => model[i].LabTest.TestName)</td>
              <td>@Html.EditorFor(model => model[i].APPROVED_BY)</td>
              <td>@Html.EditorFor(model => model[i].APPROVED_DATE)</td>
              <td>@Html.EditorFor(model => 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>
    @*<p style="color:green;font-size:16px;">
        @ViewBag.Message
    </p>*@
甜中书 2025-02-19 07:08:48

基于@RAM Anugandula逻辑进行了修改,我们可以在循环完成后保存数据,因为对象是参考类型,我们可以保存修改整个对象后

[HttpPost]
public ActionResult Edit(List<RESULT> Input)
{
   if (ModelState.IsValid)
    {
        foreach(var item in Input)
        {
            var model =_context.Yourdbsetentity.Find(item.ID);
            If(model!=null){
            model.TestID = item.TestID;
            model.TestName = item.TestName;
            //update columns you want to update
             }else{
               model=new RESULT();
               model.TestID = item.TestID;
               model.TestName = item.TestName;
              //Create columns you want to update
             _context.Yourdbsetentity.add(model)
           }
        }
            _context.SaveChanges();
        return RedirectToAction("Details");
    }
    return View(results);
}

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

[HttpPost]
public ActionResult Edit(List<RESULT> Input)
{
   if (ModelState.IsValid)
    {
        foreach(var item in Input)
        {
            var model =_context.Yourdbsetentity.Find(item.ID);
            If(model!=null){
            model.TestID = item.TestID;
            model.TestName = item.TestName;
            //update columns you want to update
             }else{
               model=new RESULT();
               model.TestID = item.TestID;
               model.TestName = item.TestName;
              //Create columns you want to update
             _context.Yourdbsetentity.add(model)
           }
        }
            _context.SaveChanges();
        return RedirectToAction("Details");
    }
    return View(results);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文