【发布时间】:2010-11-24 13:39:37
【问题描述】:
编辑:我做了一些更好的东西来使用 ViewModels 从视图中填充和读取数据,称为 ValueInjecter。 http://valueinjecter.codeplex.com/
http://prodinner.codeplex.com 使用它 - 一个 ASP.net MVC 示例应用程序
您可以在 prodinner 中看到使用 ViewModels 的最佳方式
使用 ViewModel 存储映射逻辑并不是一个好主意,因为存在重复和 SRP 违规,但现在使用 ValueInjecter,我有干净的 ViewModel 和干映射代码
那是旧东西,不要使用它:
我制作了一个 ViewModel 模式,用于在 asp.net mvc 中编辑东西 当您必须制作用于编辑实体的表单并且必须在表单上放置一些下拉菜单以供用户选择一些值时,此模式很有用
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; }
...
}
【问题讨论】:
标签: asp.net asp.net-mvc design-patterns viewmodel