2017-09-04 39 views
0

我想在ASP.Net MVC更新單場5我得到錯誤:在ASP.Net更新單場MVC 5

我的代碼:

MyJobs.PublishStatus = 1; 
    db.MyJobContext.Attach(MyJobs); 
    db.Entry(MyJobs).Property(x => x.PublishStatus).IsModified = true; 
    //db.Configuration.ValidateOnSaveEnabled = false; 
    db.SaveChanges(); 

我得到波紋管錯誤:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

沒有關於此錯誤的職位,但我無法弄清楚: Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

和在asp.net MVC更新單場:How to update only one field using Entity Framework?

+1

'MyJobs.PublishStatus = 1;'如果'MyJobs'來自LINQ結果集,可能已經更新'PublishStatus',這樣當執行Attach時,就不會再有行被更新。 –

+0

如果您將'ValidateOnSaveEnabled'設置爲false,則它將起作用。我可以看到你的代碼你已經有了這一行,你爲什麼評論它? – Munzer

+0

我試過但沒有工作 –

回答

0

我通過更新我的形式固定了這個問題,並增加了輸入隱藏Id字段

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.Id) 
    <div class="form-actions no-color"> 
     <input type="submit" value="Publish" class="btn btn-default" /> 
    </div> 
} 

希望這會幫助別人誰面臨着同樣的問題。