我目前正在使用C#学习MVC,并面临以下问题。
我已经创建了两个模型,两者都运行良好。
模型1:
public class MeetingResolutionViewModel
{
public string ResolutionNo { get; set; }
public string ResolutionCd { get; set; }
public string ResolutionDesc { get; set; }
public long EventId{ get; set; }
}
模型1的映射如下:
public static void MapToMeetingResolution(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<PxVotingResolution, MeetingResolutionViewModel>()
.ForMember(dest => dest.ResolutionId, m => m.MapFrom(src => src.Id))
.ReverseMap();
cfg.CreateMap<PxVotingElection, MeetingResolutionViewModel>()
.ReverseMap();
}
模型2:
public class ProxyVotingViewModel
{
public bool RoleUpdate { get; set; }
public bool RoleElect { get; set; }
public long EventId { get; set; }
public DateTime? LastUpdatedAt { get; set; }
}
模型2的映射如下:
public static void MapToProxyMeeting(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<EventDetail, ProxyVotingViewModel>();
}
模型1和模型2共享相同的EventId列。
我正在创建另一个视图模型,如下所示。它是以上2个模型与&#34; MeetingResolutionViewModel&#34;的组合。列表中的模型。
public class ProxyVotingEmailViewModel
{
public List<MeetingResolutionViewModel> MeetingResolutionViewModel { get; set; }
public ProxyVotingViewModel ProxyVotingViewModel { get; set; }
}
使用自动映射器,我完成了以下映射:
public static void MapToProxyVotingEmailViewModel(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<ProxyVotingViewModel, ProxyVotingEmailViewModel>();
cfg.CreateMap<MeetingResolutionViewModel, ProxyVotingEmailViewModel>();
}
在下面的函数中,如何解决评论中提到的2个问题?
public async Task<CommandResult<List<string>>> SendEmail(List<MeetingResolutionViewModel> data)
{
//blah blah
var emailData = new ProxyVotingEmailViewModel();
var pxData = new ProxyVotingViewModel();
/* I would like to do 2 things:
* 1) get pxData using the EventId from data[0];
* 2) get emailData value by combining pxData with data (as per the view model definition)
**/
//blah blah
}