我一直在寻找一下。我尝试了一些涉及捕获OptimisticConcurrency并添加:
的解决方案“@ Html.HiddenFor(model => model.OrganizationID)”
到我的删除页面。我的索引(列表),创建和编辑页面就像魅力一样。但是,当我去删除一行时,它给出了以下错误:
DbUpdateConcurrencyException
存储更新,插入或删除语句会影响意外 行数(0)。自那以后,实体可能已被修改或删除 实体已加载。刷新ObjectStateManager条目。
我已经按照教程构建了Database First应用程序。目前,我只是从我的组织表中引入数据,直到我能够顺利运行。我的组织模型看起来像这样(由“添加代码生成项”自动生成):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace VAGTC.Models
{
using System;
using System.Collections.Generic;
public partial class Organization
{
public Organization()
{
this.Contact_Title = new HashSet<Contact_Title>();
this.Organization_Address = new HashSet<Organization_Address>();
this.Organization_Business_Type = new HashSet<Organization_Business_Type>();
this.Organization_Country = new HashSet<Organization_Country>();
this.Organization_Email = new HashSet<Organization_Email>();
this.Organization_Membership = new HashSet<Organization_Membership>();
this.Organization_Notes = new HashSet<Organization_Notes>();
this.Organization_Phone = new HashSet<Organization_Phone>();
this.Organization_Website = new HashSet<Organization_Website>();
this.Contacts = new HashSet<Contact>();
this.Organization_Industry_Code = new HashSet<Organization_Industry_Code>();
}
public int OrganizationID { get; set; }
public string Name { get; set; }
public virtual ICollection<Contact_Title> Contact_Title { get; set; }
public virtual ICollection<Organization_Address> Organization_Address { get; set; }
public virtual ICollection<Organization_Business_Type> Organization_Business_Type { get; set; }
public virtual ICollection<Organization_Country> Organization_Country { get; set; }
public virtual ICollection<Organization_Email> Organization_Email { get; set; }
public virtual ICollection<Organization_Membership> Organization_Membership { get; set; }
public virtual ICollection<Organization_Notes> Organization_Notes { get; set; }
public virtual ICollection<Organization_Phone> Organization_Phone { get; set; }
public virtual ICollection<Organization_Website> Organization_Website { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual ICollection<Organization_Industry_Code> Organization_Industry_Code { get; set; }
}
}
这是我的组织控制器中的删除ActionResult:
//
// GET: /Organization/Delete/5
public ActionResult Delete(int id)
{
using (var db = new VAGTCEntities())
{
return View(db.Organizations.Find(id));
}
}
//
// POST: /Organization/Delete/5
[HttpPost]
public ActionResult Delete(int id, Organization organization)
{
try
{
// TODO: Add delete logic here
using (var db = new VAGTCEntities())
{
db.Entry(organization).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
并在我的索引页面上声明主键:
@ Html.ActionLink(“删除”,“删除”,新{id = item.OrganizationID})
出于好奇,我决定尝试添加
“@ Html.HiddenFor(model =&gt; model.OrganizationID)”
在BeginForm下面的底部,而不是在fieldset中的页面顶部和legend标签下面:
@using (Html.BeginForm()) {
<p>
@Html.HiddenFor(model => model.OrganizationID)
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
低,看哪 - 它奏效了。 我还想发这个!也许其他人会找到它,它会帮助他们。 虽然我不知道100%为什么会这样 - 有人可以解释一下这个问题吗?
答案 0 :(得分:1)
当您使用from helper
时@using (Html.BeginForm()) {
它吐出以下输出
<form>
</form>
如果你想要发布隐藏字段,你需要在表单内部获取它,否则它不会被发布,这就是当你把隐藏字段放在外面时OrganizationID
是0
的原因形式......