编辑:我使用ViewModels 从 View 中进行填充和读取数据做得更好,称为 ValueInjecter 。 http://valueinjecter.codeplex.com/
http://prodinner.codeplex.com使用它-ASP.net MVC示例应用程序
,您将看到在prodinner中使用ViewModels的最佳方法
使用ViewModel来存储映射逻辑并不是一个好主意,因为存在重复和SRP违规问题,但是现在使用ValueInjecter,我有了干净的ViewModels和干燥的映射代码
那是旧东西,不要使用它:
我制作了一个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 ViewModel模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1361092/