编辑:我做了一些更好的东西来填充和读取使用 ViewModels 的视图中的数据,称为ValueInjecter。http://valueinjecter.codeplex.com/
它由http://prodinner.codeplex.com - 一个 ASP.net MVC 示例应用程序使用
您可以在 prodinner 中看到使用 ViewModel 的最佳方式
使用 ViewModel 存储映射逻辑并不是一个好主意,因为存在重复和 SRP 违规,但现在使用 ValueInjecter 我有干净的 ViewModel 和干映射代码
那是旧的东西,不要使用它:
我在 asp.net mvc 中创建了一个 ViewModel 模式来编辑东西,当你必须制作一个表单来编辑实体并且你必须在表单上放一些东西时,这个模式很有用 - downs 供用户选择一些值
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; }
...
}