編集:ViewModelsを使用してビューからデータを入力および読み取るために、ValueInjecterと呼ばれるものを大幅に改善しました。http://valueinjecter.codeplex.com/
http://prodinner.codeplex.com(ASP.net MVCサンプルアプリケーション )で使用されます
あなたはprodinnerでViewModelsを使用する最良の方法を見ることができます
ViewModelを使用してマッピングロジックを格納することは、繰り返しとSRP違反があったため、あまり良い考えではありませんでしたが、ValueInjecterを使用すると、クリーンなViewModelとドライマッピングコードが得られます。
これは古いものです。使用しないでください
。asp.netmvcで編集するためのViewModelパターンを作成しました。このパターンは、エンティティを編集するためのフォームを作成する必要があり、フォームにドロップを配置する必要がある場合に便利です。ユーザーがいくつかの値を選択するためのダウン
public class OrganisationBadViewModel
{
//paramterless constructor required, cuz we are gonna get an OrganisationViewModel object from the form in the post save method
public OrganisationViewModel() : this(new Organisation()) {}
public OrganisationViewModel(Organisation o)
{
Organisation = o;
Country = new SelectList(LookupFacade.Country.GetAll(), "ID", "Description", CountryKey);
}
//that's the Type for whom i create the viewmodel
public Organisation Organisation { get; set; }
...
}