问题描述
假设我有以下几点:
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public virtual List<LastName> LastName { get; set; }
}
public class LastName
{
public int LastNameID { get; set; }
public new string Value1 { get; set; }
}
和我有一个索引页,其中型号为员工具有以下列表:
and I have an index page where the model is a list of employees with the following:
@foreach (var item in Model) {
//link to Edit page where I can modify the last names in the list
@Html.ActionLink(item.LastName[someIndex], "Edit", new { id = item.EmployeeID })
和控制器我有
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
//EXCEPTION HERE -
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
和一个像这样的编辑视图
and an edit view like this
@model Testing123.Models.Employee
@{
ViewBag.Title = "LastName";
}
<h2>Last Names</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@for (int i = 0; i < Model.LastName.Count; i ++)
{
@Html.TextBoxFor(m => m.LastName[i].Value1)
//ADDED HIDDEN FOR HERE**** Throwing new exception, see below
@Html.HiddenFor(m => m.LastName[i].LastNameID)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
当我提出这个编辑表单,它给了我一个例外:
When I submit this edit form, it gives me an exception :
附加型Testing123.Models.LastName的实体失败,因为同类型的另一实体已经有相同的主键值。这可以用'附加'方法或设置实体'不变'或'修改',如果图中的任何实体有冲突的关键值的状态时发生。这可能是因为一些实体是新的,但尚未收到数据库生成的键值。在这种情况下使用添加方法或添加实体状态跟踪图,然后设置非新的实体,以不变或修改适当的状态
Attaching an entity of type 'Testing123.Models.LastName' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate
这是修改姓氏复杂类型对象的列表的正确方法?
这意味着,如果有4个姓氏已经和我进入这个观点,与文本框,每个姓氏显示在一个文本框,我在文本框中更改值,将本次更新的形式提交姓氏?
Is this the correct way of modifying the List of LastName complex type object?meaning, If have 4 last names already, and I enter this view, with the text boxes, and each last name is displayed in a text box, and I change the value in the text box, will this update the last names on form submitting?
修改的
EDIT
添加HiddenFor帮手后,我得到了以下异常现在db.saveChanges
After adding the HiddenFor helper, I'm getting the following exception now on db.saveChanges
商店更新,插入或删除语句影响的行(0)的一个意想不到的数量。实体可能已被修改或删除,因为实体被装。刷新ObjectStateManager条目。
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
推荐答案
这将看看那里的异常是从(即堆栈跟踪)来是有用的,但在缺乏有几件事情我想点出去。
It would be useful to see where the exception is coming from (i.e. stack trace), but in the absence of that there are a few things I'd point out.
- 在编辑动作,你是否
员工
为空后,你已经用它来获得姓
属性。 - 编辑视图不(或之前没有编辑)提供任何机制来链接它产生回到模型的文本框。你应该使用而非
Html.TextBox
。你也需要有LastNameId
在Html.HiddenFor(M = GT; m.LastName [I] .LastNameId)引用
(内循环,很明显)和雇员
和姓
在HTML。 HiddenFor
项(外循环),以便当员工调回所有属性上它和它的子集是往返的回服务器。 -
姓
(类型)不是一个复杂的类型,因为它不具有[的ComplexType]
属性而且它也有将被推断为表中的主键的列。复杂类型和正常EF实体之间的区别可能不是回答这个问题很重要,但。看看了解详情
- In the Edit action you check if
employee
is null after you've already used it to get theLastName
property. - The Edit view does not (or didn't before the edit) provide any mechanism to link the text boxes it generates back to the model. You should use the
Html.TextBoxFor
rather thanHtml.TextBox
. Also you need to have theLastNameId
referenced in anHtml.HiddenFor(m => m.LastName[i].LastNameId)
(inside the loop, obviously) and theEmployeeId
andFirstName
inHtml.HiddenFor
entries (outside the loop) so that when the employee is posted back all of the properties on it and its child collection are round-tripped back to the server. LastName
(the type) is not a complex type because it doesn't have a[ComplexType]
attribute and it does have a column that will be inferred as the primary key of the table. This distinction between complex types and normal EF entities is probably not important to answering the question, though. Have a look at this article for more information.
这篇关于复杂类型EF6 MVC5的修改清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!