2014-05-07 62 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

方法。您可以嘗試其他方法來處理質量分配問題。