3
問題: MVC3の子モデルコレクション&モデルでは、編集アクションでEntity Framework 4.1が正しく更新されていますが、値はDBに保存されていません。親エンティティを保存するときに、含まれているコレクションのモデルの更新がDBに保存されませんでしたか?
概要: - モデルの対象者は、CaseRefの オブジェクト含まれています - 人のプロパティの更新がdb.SaveChanges上のDBに保存されて得ている()は内部コレクションはCaseRefプロパティの更新は 保存されていません - すべての値がバインドされています/ HttpPost ActionResult Edit()の入力時に正しくマップされるため、モデルはフォーム送信(Edit View)から正常に更新されます。
モデル:
public class Person
{
public Person()
{
this.CaseRefs = new HashSet<CaseRef>();
}
// <...more properties here...>
public string Name { get; set; }
public int UserId {get; set}
public virtual ICollection<CaseRef> CaseRefs { get; set; }
}
public class CaseRef
{
// <...more properties here...>
public int DescId { get; set; }
public virtual Person Person { get; set; }
}
コントローラ - 修正するには、[編集](ポスト)
[HttpPost]
public ActionResult Edit(Person p)
{
if (ModelState.IsValid)
{
// NOTE: At this point all fields from Edit form have been saved to the model
// specifically the internal CaseRefs Collection value updates.
db.Entry(p).State = EntityState.Modified;
// This is saving changes to Person.Name but not saving changes to the updated
// values in CaseRefs collection
db.SaveChanges();
return RedirectToAction("Index");
}