1

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は既存のドメインモデルを更新するのに役立ちますか?
この種の更新をどのように処理していますか?

4

1 に答える 1

2

非常に単純なアプリケーションでない限り、ビュー モデルとドメイン モデルの両方に同じモデルを使用することは非常に困難です。別のドメイン モデルを使用するもう 1 つの理由は、100 個のプロパティすべてを巨大なフォームで表示するのはユーザー フレンドリーではないことです。タスクごとに異なるフォームをユーザーに適切に表示できます。それを行うと、いずれにせよ、1 つのドメイン エンティティに対して異なるビューモデルが作成されます。

于 2010-07-13T10:25:28.207 に答える