我已经创建了表示模型,并希望将其(使用AutoMapper)映射到ViewModel中。ViewModel是复合的/因为我使用的是partials,并且我想要重用,例如,KeyboardsViewModel也可以在其他视图/partials上使用。
如何将此演示模型映射(设置映射)到ViewModel中?这是正确的方法吗?
谢谢!
public class MainPresentationModel : BasePresentationModel
{
// Should map into the MainViewModel.Keyboards.Keyboards
public int DefaultKeyboard { get; set; }
// Should map into the MainViewModel.Keyboards.DefaultKeyboard
public IList<Keyboard> Keyboards { get; set; }
// Should map into the MainViewModel.Something
public string Something { get; set; }
}
public class MainViewModel : BaseViewModel
{
public KeyboardsViewModel Keyboards { get; set; }
public string Something { get; set; }
}
public class KeyboardsViewModel
{
public int DefaultKeyboard { get; set; }
public IList<Keyboard> Keyboards { get; set; }
}编辑:在尝试之后,我认为这是一种选择:
Mapper.CreateMap<MainPresentationModel, MainViewModel>()
.ForMember(d => d.Keyboards, opt => opt.MapFrom(src => src));
Mapper.CreateMap<MainPresentationModel, KeyboardsViewModel>();它似乎是有效的,但我不确定这是否是最佳/正确的方式……
发布于 2010-01-25 21:37:17
这种方式绝对有效。您还可以使用这些复合UI的接口。例如,partial可以接受IKeyboardsViewModel,这样您就不必担心复杂的继承层次结构。然后,您可以只给每个部分一个主模型的切片。
https://stackoverflow.com/questions/2130668
复制相似问题