0

This question might sound so basic to MVVM experts but I'm trying to understand how should I know when I need to create ViewModel and how many ... OK I have learned that ViewModel is the glue between View (UI) and Model (Data) but sometimes I see an applications has 2 UIs only and one Model but then there are 5 ViewModels involved in it.

In fact, all I need to understand is what phenomenon in a project should be represented by a ViewModel?

Let's say we have a phone book app. So I'm assuming Contacts need a UI e.g. search. display, edit and delete do tell me how many UIs I need. Also,

{
 string firstName,
 string lastName,
 string phone,
 bool isCompany
}

can be a structure for the Model.

Now, when it gets to ViewModel how many ViewModels are we dealing with? and how do you recognize them?

I hope this is clear.

4

2 に答える 2

3

あなたの例を考えると、プロジェクトを次のように整理します。

  • 連絡先 [プロジェクト]
    • ビュー [フォルダ]
      • MainWindow.xaml (すべての連絡先が表示されたグリッドと、レコードを追加/編集/削除するためのツールバー)
      • CustomerInfo.xaml (Customer オブジェクトの各プロパティのフィールドを含むフォーム)
    • ViewModels [フォルダ]
      • MainWindowViewModel.cs (MainWindow の ViewModel)
      • CustomerInfoViewModel.cs (CustomerInfo の ViewModel)
    • モデル
      • Customer.cs

注意すべきことの 1 つは、新しい顧客を追加して既存の顧客を編集するロジックを処理する CustomerInfo 画面を用意することです。基本的にフォームを複製しても意味がありません。ユーザーが既存の顧客を編集しているときに、初期化時に各フィールドの値を入力するだけです。

また、グリッドの削除ボタンは、execute コマンドを呼び出して、選択したユーザーを削除できます。これは、CustomerInfo 画面からも実行できます (現在の顧客を削除するため)。

于 2012-11-05T23:49:30.943 に答える
0

私は MVC を長い間使用していませんが、私のショップでは ViewModel は、ビューに渡したいデータを構造化する方法であり、それを理解するのに役立つ方法の 1 つは、2 つを結合するために使用できることです。特定のビューで使用する複数のモデル。

モデル オブジェクトに持たせたいプロパティをセットアップします。この例では、Contact は型「Person」を返し、ContactAddress は型「Address」を返します (より明確な例になると思ったので、異なるデータ型を使用しました。何も混乱させないでください):

public class Models
{

public Person Contact
{
    // properties of first Model
    string firstName;
    string lastName;
}


public Address ContactAddress 
{
    // properties of second Model
    string Address1;
    string Address2;
    string City;
    string State;
    string Zip;
}
}// EndModels

ViewModel エンド ビューには、Contact データとその ContactAddress データが必要になることがわかっています。ViewModel を使用して、両方のデータ セットを保持し、View に渡すことができます。

public class ContactVM
{
   // properties of the ViewModel, to hold data for each Model we need to pass to the view 
   public Person Contacts {get; set;}
   public Address ContactAddresses {get;set;} 

   // Constructor
   public ContactVM()
   {
        Contacts = new Person();
        ContactAddresses = new Address();
   }
}

コントローラー コントローラーは、ViewModel を呼び出してから、各 ViewModel プロパティを呼び出して、正しいデータを設定します。これには多くの方法がありますが、例を簡単にするために除外しています。

public ActionResult Index()
{
    // create an instance of the ViewModel
    ContactVM contacts = new ContactVM();

    // make calls to populate your ViewModel properties with the correct data
    contacts.Contacts = //call to populate your Contact data here
    contacts.ContactAddresses = //call to populate your Address data here

    // pass the ViewModel to the View for use
    return View(contacts);
}
于 2012-11-05T23:54:07.353 に答える