nhibernateとaspmvc2を使用してWebアプリケーション(アプリケーションポートフォリオ)の開発を開始しました。
アプリケーションのカテゴリを適切に変更するのにいくつかの問題があります。
これが私のモデルです:
public class Application
{
public virtual int Application_ID{ get; private set; }
public virtual string Name { get; set; }
public virtual Category Category { get; set; }
}
public class Category : ILookupItem
{
public virtual int Category_ID { get; set; }
public virtual string Name { get; set; }
}
私のviewModel:
public class ApplicationEditModel
{
public Application Application { get; set; }
public SelectList Categories { get; set; }
}
私のフォーム:
<% Html.BeginForm(new {id= Model.Application.Application_ID }); %>
<table>
<tr>
<td><%=Html.LabelFor(x => x.Application.Application_ID)%></td>
<td><%=Html.DisplayFor(x=>x.Application.Application_ID) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Name) %></td>
<td><%=Html.EditorFor(x=>x.Application.Name) %></td>
</tr>
<tr>
<td><%=Html.LabelFor(x=>x.Application.Category) %></td>
<td><%=Html.DropDownListFor(x=>x.Application.Category.Category_ID,Model.Categories,"Select a category") %></td>
</tr>
<tr><td><input type="submit" /></td></tr>
</table>
<% Html.EndForm(); %>
私のコントローラーのアクション:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Application app = _service.FindById(id);
TryUpdateModel<Application>(app, "Application");
_service.CommitChanges();
return RedirectToAction("Index");
}
新しいカテゴリを割り当てることはできますが、別のカテゴリに変更すると、次のメッセージが表示されます。
Core.Model.Categoryのインスタンスの識別子が2から3に変更されました
これは、defaultmodelbinderが、新しいカテゴリに新しい新しいキーを割り当てるのではなく、割り当てられたカテゴリのキーを更新しているためと思われます。
すべての参照でエンティティを更新する正しい方法は何ですか?
カスタムビューモデルを使用し、それをコントローラーにバインドしてから、ドメインモデルにマップすることもできます。しかし、それでは作業が多すぎるのではないかと心配しています(最終的に、アプリケーションモデルには約100のプロパティ、30の参照、5〜6のリストがあります)。
この場合、Automapperは既存のドメインモデルを更新するのに役立ちますか?
この種の更新をどのように処理していますか?