2
我已經創建了演示模型,並且我想將它映射(使用AutoMapper)到ViewModel中。 ViewModel是複合/因爲我使用partials,我想在其他視圖/ partials上重用例如KeyboardsViewModel。使用AutoMapper的地圖視圖模型/部分
如何映射(設置映射)此演示文稿模型到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>();
看來,它的工作原理,但我不知道這是否是最佳/正確的方法...