2014-05-07 61 views
0

我试图更新实体,但它给我像“商店更新,插入一个错误,或者删除语句影响(0)。实体可能已被修改行的意外数字或自实体加载后删除。刷新ObjectStateManager条目。“ 我搜索了互联网,但找不到任何合适的解决方案。我想共用代码:无法在MVC更新模型4

操作方法:

[HttpPost, ValidateAntiForgeryToken] 
     public async Task<ActionResult> EditCustomTicket([Bind(Include = @"TicketDetailId,GenericOrderId,PartId,Quantity,CustomerPrice, 
                      Status,RowVersion,PersonID,Notes,Manufacturer,DateCode, 
                      Package,CustomQuantity,BuyingPrice,CustomPrice,LTDays, 
                      Description")] TicketDetail orderdetail, int? id, string request) 
     { 
      TryUpdateModel(orderdetail); 
      try 
      { 

       if (ModelState.IsValid) 
       { 
        db.TicketDetails.Attach(orderdetail); 
        var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager; 

        manager.ChangeObjectState(orderdetail, EntityState.Modified); 

        await db.SaveChangesAsync(); 
        return RedirectToAction("Details", new { id = id }); 
       } 

      } 

      catch (Exception ex) 
      { 
       ModelState.AddModelError("", ex.Message); 
       TempData["Error"] = string.Format(ex.Message + "\n" + ex.Source); 
       return RedirectToAction("Details", new { id = id }); 
      } 
      return View(orderdetail); 
     } 

这是视图:

<div> 
       @foreach (var item in Model) 
       { 
        <form method="post" action="@Url.Action("EditCustomTicket")"> 

         @Html.AntiForgeryToken() 
         @Html.ValidationSummary(true) 

         @Html.HiddenFor(model => item.TicketDetailId) 
         @Html.HiddenFor(model => item.GenericOrderId) 
         @Html.HiddenFor(model => item.Notes) 
         @Html.HiddenFor(model => item.Status) 
         @Html.HiddenFor(model => item.PersonID) 
         @Html.HiddenFor(model => item.RowVersion) 
         @Html.HiddenFor(model => item.Quantity) 
         @Html.HiddenFor(model => item.CustomerPrice) 
         @Html.HiddenFor(model => item.PartId) 


         <div class="form-group float"> 
          @Html.LabelFor(model => item.Part.PartNumber, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.DisplayFor(modelitem => item.Part.PartNumber) 
          </div> 
         </div> 

         <div class="form-group float"> 
          @Html.LabelFor(model => item.Manufacturer, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.Manufacturer, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.Manufacturer) 
          </div> 
         </div> 
         <div class="form-group float"> 
          @Html.LabelFor(model => item.DateCode, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.DateCode, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.DateCode) 
          </div> 
         </div> 

         <div class="form-group float"> 
          @Html.LabelFor(model => item.Package, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.Package, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.Package) 
          </div> 
         </div> 
         <div class="form-group float"> 
          @Html.LabelFor(model => item.Quantity, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.DisplayFor(modelitem => item.Quantity) 
          </div> 
         </div> 


         <div class="form-group float"> 
          @Html.LabelFor(model => item.Package, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.Package, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.Package) 
          </div> 
         </div> 

         <div class="form-group float"> 
          @Html.LabelFor(model => item.BuyingPrice, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.BuyingPrice, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.BuyingPrice) 
          </div> 
         </div> 


         <span id="bpusd"></span> 

         <div class="form-group float"> 
          @Html.LabelFor(model => item.CustomPrice, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.CustomPrice, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.CustomPrice) 
          </div> 
         </div> 
         <span id="amtusd"></span> 
         <div class="form-group float"> 
          @Html.LabelFor(model => item.LTDays, new { @class = "control-label col-md-2 lab" }) 
          <div class="col-md-10"> 
           @Html.EditorFor(model => item.LTDays, new { htmlattributes = new { @class = "textbox" } }) 
           @Html.ValidationMessageFor(model => item.LTDays) 
          </div> 
         </div> 

         <br /> 


         <input class="btn green butt" value="Send Quote" type="submit" /> 
         <input class="btn yellow butt" value="Send Performa INV." type="submit" /> 
         <input class="btn blue butt" value="Send Invoice" type="submit" /> 



         <hr /> 
        </form> 
       } 
      </div> 

它是一个局部视图。请用这个指导我。

+0

唯一的例外大概意思是说,你装上'orderdetail'的关键属性('orderdetail.TicketDetailId'?抑或是关键,即使复合?)没有在数据库中的现有记录的正确值。你应该在调试器中进行调查。 – Slauma

+0

@Slauma它具有正确的值,我已在调试器中检查了它。我如何解决这个问题?我已添加行版本属性以避免乐观的并发冲突 – Anony

+0

并且在您附加之前正确设置了RowVersion属性?你作为RowVersion使用了什么?一个字节数组?还是自定义? – Slauma

回答

0

当数据没有改变这可能发生或不包括主键字段。

+0

我已包括主键字段,我已更新问题请检查 – Anony

0

您还没有安装到现有实体。

你需要指定它是哪一个实体,你有才能例如附加票细节,它的工作

var existingTicket = db.TicketDetails.Find(Id); 

existingTicket.Attach(orderdetail); 
0

尝试删除绑定属性,在

EditCustomTicket

方法。您可以尝试其他方法来处理质量分配问题。